<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：shop.guanjunbang.cn
 * 授权域名：shop.guanjunbang.cn
 * 授权码：vdcajlVLIVAgVdkfJvgY
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

namespace app\controller;
use think\facade\Db;

class ApiShop extends ApiCommon {
	public function getprolist() {
		$where = [];
		$where[] = ['aid', '=', aid];
		$where[] = ['ischecked', '=', 1];
		if (isdouyin == 1) {
			$where[] = ['douyin_product_id', '<>', ''];
		} else {
			$where[] = ['douyin_product_id', '=', ''];
		}
		//$where[] = ['status','=',1];
		$nowtime = time();
		$nowhm = date('H:i');
		$where[] = Db::raw("`status`=1 or (`status`=2 and unix_timestamp(start_time)<=$nowtime and unix_timestamp(end_time)>=$nowtime) or (`status`=3 and ((start_hours<end_hours and start_hours<='$nowhm' and end_hours>='$nowhm') or (start_hours>=end_hours and (start_hours<='$nowhm' or end_hours>='$nowhm'))) )");

		if (input('param.field') && input('param.order')) {
			$order = input('param.field') . ' ' . input('param.order') . ',sort desc,id desc';
		} else {
			$order = 'sort desc,id desc';
		}
		if (input('param.bid')) {
			$where[] = ['bid', '=', input('param.bid/d')];
		} else {
			$business_sysset = Db::name('business_sysset')->where('aid', aid)->find();
			if (!$business_sysset || $business_sysset['status'] == 0 || $business_sysset['product_isshow'] == 0) {
				$where[] = ['bid', '=', 0];
			} else {
				if (getcustom('prolist_showjuli') && input('param.cid')) {
					$latitude = input('param.latitude');
					$longitude = input('param.longitude');
					if ($longitude && $latitude) {
						$border = Db::raw("({$longitude}-longitude)*({$longitude}-longitude) + ({$latitude}-latitude)*({$latitude}-latitude) ");
						$blist = Db::name('business')->where('aid', aid)->where('status', 1)->where("longitude!='' and latitude!=''")->field('id,longitude,latitude')->order($border)->select()->toArray();
						$bids = [];
						$bjuli = [];
						$b0juli = getdistance($longitude, $latitude, $this->sysset['longitude'], $this->sysset['latitude'], 2);
						foreach ($blist as $binfo) {
							$juli = getdistance($longitude, $latitude, $binfo['longitude'], $binfo['latitude'], 2);
							if ($juli > $b0juli && !in_array('0', $bids)) {
								$bids[] = '0';
								$bjuli['0'] = '' . $b0juli . 'km';
							}
							$bids[] = $binfo['id'];
							$bjuli['' . $binfo['id']] = '' . $juli . 'km';
						}
						if (!input('param.field') || input('param.field') == 'sort') {
							$order = Db::raw('field(bid,' . implode(',', $bids) . '),sort desc,id desc');
						}
					}
				}
			}
		}

		//定位
		if (input('param.city')) {
			$blist = Db::name('business')->where('aid', aid)->where('status', 1)->where('city', input('param.city'))->field('id')->order($border)->select()->toArray();
			$ids = array_column($blist, 'id');
			$where[] = ['bid', 'in', $ids];

		}
		//分类
		if (input('param.cid')) {
			$cid = input('post.cid') ? input('post.cid/d') : input('param.cid/d');
			$where2 = "find_in_set('-1',showtj)";
			if ($this->member) {
				$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
				if ($this->member['subscribe'] == 1) {
					$where2 .= " or find_in_set('0',showtj)";
				}
			}
			$tjwhere[] = Db::raw($where2);
			//子分类
			$clist = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', $cid)->column('id');
			if ($clist) {
				$clist2 = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', 'in', $clist)->column('id');
				$cCate = array_merge($clist, $clist2, [$cid]);
				if ($cCate) {
					$whereCid = [];
					foreach ($cCate as $k => $c2) {
						$whereCid[] = "find_in_set({$c2},cid)";
					}
					$where[] = Db::raw(implode(' or ', $whereCid));
				}
			} else {
				$where[] = Db::raw("find_in_set(" . $cid . ",cid)");
			}
		} else {
			if (getcustom('product_cat_showtj')) {
				$where2 = "find_in_set('-1',showtj)";
				if ($this->member) {
					$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
					if ($this->member['subscribe'] == 1) {
						$where2 .= " or find_in_set('0',showtj)";
					}
				}
				$tjwhere[] = Db::raw($where2);
				$clist = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->column('id');
				if ($clist) {
					$whereCid = [];
					foreach ($clist as $k => $c2) {
						$whereCid[] = "find_in_set({$c2},cid)";
					}
					$where[] = Db::raw(implode(' or ', $whereCid));
				}
			}
		}

		//商家的商品分类
		if (input('param.cid2')) {
			$cid2 = input('post.cid2') ? input('post.cid2/d') : input('param.cid2/d');
			//子分类
			$clist = Db::name('shop_category2')->where('aid', aid)->where('pid', $cid2)->column('id');
			if ($clist) {
				$clist2 = Db::name('shop_category2')->where('aid', aid)->where('pid', 'in', $clist)->column('id');
				$cCate = array_merge($clist, $clist2, [$cid2]);
				if ($cCate) {
					$whereCid = [];
					foreach ($cCate as $k => $c2) {
						$whereCid[] = "find_in_set({$c2},cid2)";
					}
					$where[] = Db::raw(implode(' or ', $whereCid));
				}
			} else {
				$where[] = Db::raw("find_in_set(" . $cid2 . ",cid2)");
			}
		}
		if (input('param.gid')) {
			$where[] = Db::raw("find_in_set(" . intval(input('param.gid')) . ",gid)");
		}

		if (input('param.proparams')) {
			$proparams = input('param.proparams');
			$whereparam = [];
			foreach ($proparams as $paramkey => $paramval) {
				if (!$paramval) {
					continue;
				}

				$whereparam[] = "paramdata like '%" . '"' . $paramkey . '":"' . $paramval . '"' . "%'";
			}
			if ($whereparam) {
				$where[] = Db::raw(implode(' and ', $whereparam));
			}
		}

		if (input('param.keyword')) {
			$where[] = ['name', 'like', '%' . input('param.keyword') . '%'];
		}

		$where2 = "find_in_set('-1',showtj)";
		if ($this->member) {
			$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
			if ($this->member['subscribe'] == 1) {
				$where2 .= " or find_in_set('0',showtj)";
			}
		}
		$where[] = Db::raw($where2);

		//优惠券可用商品列表
		$cpid = input('param.cpid/d');
		if ($cpid > 0) {
			$coupon = Db::name('coupon')->where('id', $cpid)->find();
			$where[] = ['bid', '=', $coupon['bid']];
			if ($coupon['fwtype'] == 1) {
				//指定类目
				$categoryids = explode(',', $coupon['categoryids']);
				$clist = Db::name('shop_category')->where('pid', 'in', $categoryids)->select()->toArray();
				foreach ($clist as $kc => $vc) {
					$categoryids[] = $vc['id'];
					$cate2 = Db::name('shop_category')->where('pid', $vc['id'])->find();
					$categoryids[] = $cate2['id'];
				}
				$whereCid = [];
				foreach ($categoryids as $k => $c2) {
					$whereCid[] = "find_in_set({$c2},cid)";
				}
				$where[] = Db::raw(implode(' or ', $whereCid));
			}
			if ($coupon['fwtype'] == 2) {
				//指定商品
				$where[] = ['id', 'in', $coupon['productids']];
			}
		}
		$pernum = 10;
		$pagenum = input('post.pagenum');
		if (!$pagenum) {
			$pagenum = 1;
		}

		$field = 'id,bid,pic,name,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,price_type,stock';
		if (getcustom('plug_tengrui')) {
			$field .= ',house_status,group_status,group_ids,is_rzh,relation_type';
		}
		if (getcustom('shop_other_infor')) {
			$field .= ',xunjia_text';
		}
		$datalist = Db::name('shop_product')->field($field)->where($where)->page($pagenum, $pernum)
		->order($order)->select()->toArray();
//		$sql = Db::getlastsql();dd($sql);
		if (!$datalist) {
			$datalist = [];
		}else{
				foreach ($datalist as $key => $value) {
						$shop = Db::name('business')->where('id', $value['bid'])->find();
						$datalist[$key]['shop_name'] = $shop['name'];
						$datalist[$key]['logo'] = $shop['logo'];
					}
		}

		if (getcustom('prolist_showjuli') && input('param.cid') && $bjuli) {
			foreach ($datalist as $k => $v) {
				$datalist[$k]['juli'] = $bjuli['' . $v['bid']];
			}
		}
		if (getcustom('plug_tengrui')) {
			if ($datalist) {
				$tr_check = new \app\common\TengRuiCheck();
				foreach ($datalist as $dk => $dv) {
					//判断是否是否符合会员认证、会员关系、一户、用户组，不符合则直接去掉
					$check_product = $tr_check->check_product($this->member, $dv, 1);
					if ($check_product && $check_product['status'] == 0) {
						unset($datalist[$dk]);
					}
				}
				unset($dv);
				$len = count($datalist);
				if ($len < 10) {
					//重置索引,防止上方去掉的数据产生空缺
					$datalist = array_values($datalist);
				}
			}
		}
		if (getcustom('shop_other_infor')) {
			if ($datalist) {
				$sysset = Db::name('admin_set')->where('aid', aid)->field('name,main_business,tel')->find();
				foreach ($datalist as &$v) {
					//特别标识
					$v['is_soi'] = 1;

					//联系系统名称
					$v['lx_name'] = $sysset['name'] ? $sysset['name'] : '';
					//联系商家id
					$v['lx_bid'] = $v['bid'];
					//联系商家名称
					$v['lx_bname'] = '';
					//联系电话
					$v['lx_tel'] = '';

					$v['merchant_name'] = '';
					$v['main_business'] = '';
					//查询商家
					if ($v['bid'] > 0) {
						$merchant_name = Db::name('business')
							->where('id', $v['bid'])
							->where('aid', aid)
							->field('name,main_business,tel')
							->find();
						if ($merchant_name) {
							$v['merchant_name'] = $merchant_name['name'];
							$v['main_business'] = $merchant_name['main_business'];

							//联系商家名称
							$v['lx_bname'] = $merchant_name['name'] ? $merchant_name['name'] : '';
							//联系电话
							$v['lx_tel'] = $merchant_name['tel'] ? $merchant_name['tel'] : '';
						}
					} else {
						$v['merchant_name'] = $sysset['name'];
						$v['main_business'] = $sysset['main_business'];

						//联系电话
						$v['lx_tel'] = $sysset['tel'] ? $sysset['tel'] : '';
					}

				}
				unset($v);
			}
		}
		$datalist = $this->formatprolist($datalist);
		return $this->json(['status' => 1, 'data' => $datalist]);
	}
	public function prolist() {
		$where2 = "find_in_set('-1',showtj)";
		if ($this->member) {
			$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
			if ($this->member['subscribe'] == 1) {
				$where2 .= " or find_in_set('0',showtj)";
			}
		}
		$tjwhere[] = Db::raw($where2);
		//分类
		if (input('param.cid')) {
			$clist = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', input('param.cid/d'))->where('status', 1)->order('sort desc,id')->select()->toArray();
			if (!$clist) {
				$clist = [];
			}

		} else {
			$clist = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', 0)->where('status', 1)->order('sort desc,id')->select()->toArray();
			if (!$clist) {
				$clist = [];
			}

		}
		if (input('param.bid')) {
			$bid = input('param.bid/d');
			if (input('param.cid2')) {
				$clist2 = Db::name('shop_category2')->where('aid', aid)->where('bid', $bid)->where('pid', input('param.cid2/d'))->where('status', 1)->order('sort desc,id')->select()->toArray();
				if (!$clist2) {
					$clist2 = [];
				}

			} else {
				$clist2 = Db::name('shop_category2')->where('aid', aid)->where('bid', $bid)->where('pid', 0)->where('status', 1)->order('sort desc,id')->select()->toArray();
				if (!$clist2) {
					$clist2 = [];
				}

			}
		} else {
			$clist2 = [];
		}
		//分组
		$glist = Db::name('shop_group')->where('aid', aid)->where('status', 1)->select()->toArray();
		if (!$glist) {
			$glist = [];
		}

		$rsset = [];
		if (getcustom('image_search')) {
			$image_search = Db::name('admin')->where('id', aid)->value('image_search');
			if ($image_search == 1) {
				$set = Db::name('baidu_set')->where('aid', aid)->find();
				$rsset['image_search'] = $set['image_search'];
			}
		}
		$topshowcategory = 0;
		if (getcustom('ngmm')) {
			$topshowcategory = 1;
			if (!$clist && input('param.cid')) {
				$thiscinfo = Db::name('shop_category')->where('aid', aid)->where('id', input('param.cid'))->find();
				$clist = Db::name('shop_category')->where('aid', aid)->where('pid', $thiscinfo['pid'])->where('status', 1)->order('sort desc,id')->select()->toArray();
			}
		}

		$rdata = [];
		$rdata['clist'] = $clist;
		$rdata['glist'] = $glist;
		$rdata['clist2'] = $clist2;
		$rdata['set'] = $rsset;
		$rdata['topshowcategory'] = $topshowcategory;

		if (getcustom('prolist_showjuli')) {
			$rdata['needlocation'] = true;
		}

		return $this->json($rdata);
	}
	public function getparamlist() {
		//参数
		$whereParam = [];
		$whereParam[] = ['aid', '=', aid];
		$whereParam[] = ['status', '=', 1];
		$whereParam[] = ['type', '=', 1];
		if (input('param.bid')) {
			$bid = input('param.bid/d');
		} else {
			$bid = 0;
		}
		if (input('param.cid')) {
			$cid = input('param.cid');
			if ($cid) {
				if (getcustom('business_showplatparam') && $bid > 0) {
					$whereParam[] = Db::raw("(bid=0 and find_in_set({$cid},cid)) or (bid={$bid} and find_in_set({$cid},cid) or cid =''))");
				} else {
					$whereParam[] = ['bid', '=', $bid];
					$whereParam[] = Db::raw("find_in_set({$cid},cid) or cid =''");
				}
			} else {
				$whereParam[] = ['bid', '=', $bid];
				$whereParam[] = Db::raw("cid =''");
			}
		} else {
			$whereParam[] = ['bid', '=', $bid];
			$whereParam[] = Db::raw(" cid =''");
		}
		$paramList = Db::name('shop_param')->where($whereParam)->order('sort desc,id')->select()->toArray();
		if (!$paramList) {
			$paramList = [];
		}

		foreach ($paramList as $k => $v) {
			$paramList[$k]['params'] = json_decode($v['params'], true);
		}
		$rdata = [];
		$rdata['status'] = 1;
		$rdata['data'] = $paramList;
		return $this->json($rdata);
	}

	//分类商品
	public function classify() {
		if (input('param.bid')) {
			$clist = Db::name('shop_category2')->where('aid', aid)->where('bid', input('param.bid/d'))->where('pid', 0)->where('status', 1)->order('sort desc,id')->select()->toArray();
			foreach ($clist as $k => $v) {
				$rs = Db::name('shop_category2')->where('aid', aid)->where('pid', $v['id'])->where('status', 1)->order('sort desc,id')->select()->toArray();
				if (!$rs) {
					$rs = [];
				}

				$clist[$k]['child'] = $rs;
			}
		} else {
			$where2 = "find_in_set('-1',showtj)";
			if ($this->member) {
				$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
				if ($this->member['subscribe'] == 1) {
					$where2 .= " or find_in_set('0',showtj)";
				}
			}
			$tjwhere[] = Db::raw($where2);
			$clist = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', 0)->where('status', 1)->order('sort desc,id')->select()->toArray();
//			dd($clist);
			foreach ($clist as $k => $v) {
				$rs = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', $v['id'])->where('status', 1)->order('sort desc,id')->select()->toArray();
				if (!$rs) {
					$rs = [];
				}

				$clist[$k]['child'] = $rs;
			}
		}
		return $this->json(['status' => 1, 'data' => $clist]);
	}
	//获取子分类
	public function getdownclist() {
		$pid = input('param.id/d');
		$clist = Db::name('shop_category')->where('aid', aid)->where('pid', $pid)->where('status', 1)->order('sort desc,id')->select()->toArray();
		if (!$clist) {
			$clist = [];
		}

		return $this->json(['status' => 1, 'data' => $clist]);
	}
	//分类商品样式二
	public function classify2() {
		if (input('param.bid')) {
			$bid = input('param.bid/d');
			$clist = Db::name('shop_category2')->where('aid', aid)->where('bid', $bid)->where('pid', 0)->where('status', 1)->order('sort desc,id')->select()->toArray();
			foreach ($clist as $k => $v) {
				$child = Db::name('shop_category2')->where('aid', aid)->where('bid', $bid)->where('pid', $v['id'])->where('status', 1)->order('sort desc,id')->select()->toArray();
				if (!$child) {
					$child = [];
				}

				foreach ($child as $k2 => $v2) {
					$child2 = Db::name('shop_category2')->where('aid', aid)->where('bid', $bid)->where('pid', $v2['id'])->where('status', 1)->order('sort desc,id')->select()->toArray();
					$child[$k2]['child'] = $child2;
				}
				$clist[$k]['child'] = $child;
			}
		} else {
			$where2 = "find_in_set('-1',showtj)";
			if ($this->member) {
				$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
				if ($this->member['subscribe'] == 1) {
					$where2 .= " or find_in_set('0',showtj)";
				}
			}
			$tjwhere[] = Db::raw($where2);
			$clist = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', 0)->where('status', 1)->order('sort desc,id')->select()->toArray();
			foreach ($clist as $k => $v) {
				$child = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', $v['id'])->where('status', 1)->order('sort desc,id')->select()->toArray();
				if (!$child) {
					$child = [];
				}

				foreach ($child as $k2 => $v2) {
					$child2 = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', $v2['id'])->where('status', 1)->order('sort desc,id')->select()->toArray();
					$child[$k2]['child'] = $child2;
				}
				$clist[$k]['child'] = $child;
			}
		}
		return $this->json(['status' => 1, 'data' => $clist]);
	}

	//一级分类
	public function category1() {
		if (input('param.bid')) {
			$list = Db::name('shop_category2')->where('aid', aid)->where('bid', input('param.bid/d'))->where('pid', 0)->where('status', 1)->order('sort desc,id')->select()->toArray();
		} else {
			$where2 = "find_in_set('-1',showtj)";
			if ($this->member) {
				$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
				if ($this->member['subscribe'] == 1) {
					$where2 .= " or find_in_set('0',showtj)";
				}
			}
			$tjwhere[] = Db::raw($where2);
			$list = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', 0)->where('status', 1)->order('sort desc,id')->select()->toArray();
		}
		return $this->json(['status' => 1, 'data' => $list]);
	}
	//二级分类
	public function category2() {
		if (input('param.bid')) {
			$cid = input('param.cid/d');
			$bid = input('param.bid/d');
			if ($cid) {
				$rs = Db::name('shop_category2')->where('aid', aid)->where('bid', $bid)->where('pid', $cid)->where('status', 1)->order('sort desc,id')->select()->toArray();
				if (!$rs) {
					$rs = array();
				}

				$list = $rs;
			} else {
				$list = Db::name('shop_category2')->where('aid', aid)->where('bid', $bid)->where('pid', 0)->where('status', 1)->order('sort desc,id')->select()->toArray();
				if (!$list) {
					$list = array();
				}

				foreach ($list as $k => $v) {
					$rs = Db::name('shop_category2')->where('aid', aid)->where('bid', $bid)->where('pid', $v['id'])->where('status', 1)->order('sort desc,id')->select()->toArray();
					if (!$rs) {
						$rs = array();
					}

					$list[$k]['child'] = $rs;
				}
			}
		} else {
			$cid = input('param.cid/d');
			$where2 = "find_in_set('-1',showtj)";
			if ($this->member) {
				$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
				if ($this->member['subscribe'] == 1) {
					$where2 .= " or find_in_set('0',showtj)";
				}
			}
			$tjwhere[] = Db::raw($where2);
			if ($cid) {
				$rs = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', $cid)->where('status', 1)->order('sort desc,id')->select()->toArray();
				if (!$rs) {
					$rs = array();
				}

				$list = $rs;
			} else {
				$list = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', 0)->where('status', 1)->order('sort desc,id')->select()->toArray();
				if (!$list) {
					$list = array();
				}

				foreach ($list as $k => $v) {
					$rs = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', $v['id'])->where('status', 1)->order('sort desc,id')->select()->toArray();
					if (!$rs) {
						$rs = array();
					}

					$list[$k]['child'] = $rs;
				}
			}
		}
		return $this->json(['status' => 1, 'data' => $list]);
	}
	//三级分类
	public function category3() {
		if (input('param.bid')) {
			$bid = input('param.bid/d');
			$list = Db::name('shop_category2')->where('aid', aid)->where('bid', $bid)->where('pid', 0)->where('status', 1)->order('sort desc,id')->select()->toArray();
		} else {
			$where2 = "find_in_set('-1',showtj)";
			if ($this->member) {
				$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
				if ($this->member['subscribe'] == 1) {
					$where2 .= " or find_in_set('0',showtj)";
				}
			}
			$tjwhere[] = Db::raw($where2);
			$list = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', 0)->where('status', 1)->order('sort desc,id')->select()->toArray();
		}
		$rdata = [];
		$rdata['data'] = $list;
		return $this->json($rdata);
	}
	//获取二三分类
	public function getdownclist3() {
		$pid = input('param.id/d');
		if (input('param.bid')) {
			$bid = input('param.bid/d');
			$clist = Db::name('shop_category2')->where('aid', aid)->where('bid', $bid)->where('pid', $pid)->where('status', 1)->order('sort desc,id')->select()->toArray();
			if (!$clist) {
				$clist = [];
			}

			foreach ($clist as $k => $v) {
				$rs = Db::name('shop_category2')->where('aid', aid)->where('bid', $bid)->where('pid', $v['id'])->where('status', 1)->order('sort desc,id')->select()->toArray();
				if (!$rs) {
					$rs = array();
				}

				$clist[$k]['child'] = $rs;
			}
		} else {
			$clist = Db::name('shop_category')->where('aid', aid)->where('pid', $pid)->where('status', 1)->order('sort desc,id')->select()->toArray();
			if (!$clist) {
				$clist = [];
			}

			foreach ($clist as $k => $v) {
				$rs = Db::name('shop_category')->where('aid', aid)->where('pid', $v['id'])->where('status', 1)->order('sort desc,id')->select()->toArray();
				if (!$rs) {
					$rs = array();
				}

				$clist[$k]['child'] = $rs;
			}
		}
		return $this->json(['status' => 1, 'data' => $clist]);
	}

	//商品
	public function product() {
		$proid = input('param.id/d');
		$product = Db::name('shop_product')->where('id', $proid)->where('aid', aid)->find();
		if (!$product) {
			return $this->json(['status' => 0, 'msg' => '商品不存在']);
		}

		if ($product['status'] == 0) {
			return $this->json(['status' => 0, 'msg' => '商品未上架']);
		}

		if ($product['ischecked'] != 1) {
			return $this->json(['status' => 0, 'msg' => '商品未审核']);
		}

		if ($product['status'] == 2 && (strtotime($product['start_time']) > time() || strtotime($product['end_time']) < time())) {
			return $this->json(['status' => 0, 'msg' => '商品未上架']);
		}
		if ($product['status'] == 3) {
			$start_time = strtotime(date('Y-m-d ' . $product['start_hours']));
			$end_time = strtotime(date('Y-m-d ' . $product['end_hours']));
			if (($start_time < $end_time && ($start_time > time() || $end_time < time())) || ($start_time >= $end_time && ($start_time > time() && $end_time < time()))) {
				return $this->json(['status' => 0, 'msg' => '商品未上架']);
			}
		}

		$product['paramdata'] = json_decode($product['paramdata'], true);
		if (!$product['paramdata']) {
			$product['paramdata'] = [];
		}

		foreach ($product['paramdata'] as $k => $v) {
			if (is_array($v)) {
				$product['paramdata'][$k] = implode(' ', $v);
			}
			if (!$v) {
				unset($product['paramdata'][$k]);
			}

		}

		//显示条件
		if ($product['showtj'] > 0) {
			$this->checklogin();
			//限制等级
			$levelids = explode(',', $product['showtj']);
			if (!in_array($this->member['levelid'], $levelids)) {
				return $this->json(['status' => 0, 'msg' => '商品状态不可见']);
			}
		} elseif ($product['showtj'] == 0) {
			$this->checklogin();
			//关注用户
			if ($this->member['subscribe'] != 1) {
				return $this->json(['status' => 0, 'msg' => '商品状态不可见']);
			}
		}
		//商品分类显示条件
		if (getcustom('product_cat_showtj')) {
			$cidArr = explode(',', $product['cid']);
			if ($cidArr) {
				$clist = Db::name('shop_category')->where('id', 'in', $cidArr)->where('aid', aid)->select()->toArray();
				foreach ($clist as $k => $crow) {
					if ($crow['showtj'] > 0) {
						$this->checklogin();
						//限制等级
						$levelids = explode(',', $crow['showtj']);
						if (!in_array($this->member['levelid'], $levelids)) {
							return $this->json(['status' => 0, 'msg' => '商品分类状态不可见']);
						}
					} elseif ($crow['showtj'] == 0) {
						$this->checklogin();
						//关注用户
						if ($this->member['subscribe'] != 1) {
							return $this->json(['status' => 0, 'msg' => '商品分类状态不可见']);
						}
					}
				}
			}

			$where2 = "find_in_set('-1',showtj)";
			if ($this->member) {
				$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
				if ($this->member['subscribe'] == 1) {
					$where2 .= " or find_in_set('0',showtj)";
				}
			}
			$tjwhere[] = Db::raw($where2);
			$clist = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->column('id');
			if ($clist) {
				foreach ($clist as $k => $c2) {
					$whereCid[] = "find_in_set({$c2},cid)";
				}
				$where[] = Db::raw(implode(' or ', $whereCid));
			}
		}
		if (getcustom('pay_yuanbao')) {
			$yuanbao_money_ratio = 0;
			$yb_set = Db::name('admin_set')->where('aid', aid)->field('yuanbao_money_ratio')->find();
			if ($yb_set) {
				$yuanbao_money_ratio = $yb_set['yuanbao_money_ratio'];
			}
			$need_money = $product['yuanbao'] * $yuanbao_money_ratio / 100;
			$need_money = round($need_money, 2);
			$product['yuanbao'] = $product['yuanbao'] . t('元宝');
		} else {
			$product['yuanbao'] = '';
		}

		if (getcustom('plug_tengrui')) {
			//判断是否是否符合会员认证、会员关系、一户、用户组
			$tr_check = new \app\common\TengRuiCheck();
			$check_product = $tr_check->check_product($this->member, $product, 1);
			if ($check_product && $check_product['status'] == 0) {
				return $this->json(['status' => $check_product['status'], 'msg' => $check_product['msg']]);
			}
			$tr_roomId = $check_product['tr_roomId'];
		}

		$product['isgm'] = true;
		if (getcustom('shopcate_time')) {
			$procids = explode(',', $product['cid']);
			$cate = Db::name('shop_category')->where('aid', aid)->where('id', 'in', $product['cid'])->select()->toArray();
			foreach ($cate as $c) {
				if ($c['start_hours'] && $c['end_hours']) {
					$catestart_time = strtotime(date('Y-m-d ' . $c['start_hours']));
					$cateend_time = strtotime(date('Y-m-d ' . $c['end_hours']));
					if (($catestart_time < $cateend_time && ($catestart_time > time() || $cateend_time < time())) || ($catestart_time >= $cateend_time && ($catestart_time > time() && $cateend_time < time()))) {
						$product['isgm'] = false;
					}
				}

			}
		}

		if ($product['status'] == 2 || $product['status'] == 3) {
			$product['status'] = 1;
		}

		if (!$product['pics']) {
			$product['pics'] = $product['pic'];
		}

		$product['pics'] = explode(',', $product['pics']);
		$oldproduct = $product;
		$product = $this->formatproduct($product);

		if ($product['video']) {
			$minute = floor($product['video_duration'] / 60);
			if ($minute < 10) {
				$minute = '0' . $minute;
			}

			$second = $product['video_duration'] % 60;
			if ($second < 10) {
				$second = '0' . $second;
			}

			$product['video_duration'] = $minute . "'" . $second . '"';
		}

		//是否收藏
		$rs = Db::name('member_favorite')->where('aid', aid)->where('mid', mid)->where('proid', $proid)->where('type', 'shop')->find();
		if ($rs) {
			$isfavorite = true;
		} else {
			$isfavorite = false;
		}
		//获取评论
		$commentlist = Db::name('shop_comment')->where('aid', aid)->where('proid', $proid)->where('status', 1)->order('id desc')->limit(10)->select()->toArray();
		if (!$commentlist) {
			$commentlist = [];
		}

		foreach ($commentlist as $k => $pl) {
			$commentlist[$k]['createtime'] = date('Y-m-d H:i', $pl['createtime']);
			if ($commentlist[$k]['content_pic']) {
				$commentlist[$k]['content_pic'] = explode(',', $commentlist[$k]['content_pic']);
			}

		}
		$commentcount = Db::name('shop_comment')->where('aid', aid)->where('proid', $proid)->where('status', 1)->count();
		//添加浏览历史
		if (mid) {
			$rs = Db::name('member_history')->where('aid', aid)->where('mid', mid)->where('proid', $proid)->where('type', 'shop')->find();
			if ($rs) {
				Db::name('member_history')->where('id', $rs['id'])->update(['createtime' => time()]);
			} else {
				Db::name('member_history')->insert(['aid' => aid, 'mid' => mid, 'proid' => $proid, 'type' => 'shop', 'createtime' => time()]);
			}
		}

		$sysset = Db::name('admin_set')->where('aid', aid)->field('name,logo,desc,fxjiesuantype,tel,kfurl,gzts,ddbb')->find();
		$shopset = Db::name('shop_sysset')->where('aid', aid)->field('showjd,comment,showcommission,hide_sales,hide_stock,show_lvupsavemoney')->find();
		if ($this->member) {
			$userlevel = Db::name('member_level')->where('aid', aid)->where('id', $this->member['levelid'])->find();
		}
		//预计佣金
		$commission = 0;
		$product['commission_desc'] = '元';
		if ($this->member && $shopset['showcommission'] == 1) {
			if ($userlevel['can_agent'] != 0) {
				if ($product['commissionset'] == 1) {
//按比例
					$commissiondata = json_decode($product['commissiondata1'], true);
					if ($commissiondata) {
						$commission = $commissiondata[$userlevel['id']]['commission1'] * ($product['sell_price_origin'] - ($sysset['fxjiesuantype'] == 2 ? $product['cost_price'] : 0)) * 0.01;
					}
				} elseif ($product['commissionset'] == 2) {
//按固定金额
					$commissiondata = json_decode($product['commissiondata2'], true);
					if ($commissiondata) {
						$commission = $commissiondata[$userlevel['id']]['commission1'];
					}
				} elseif ($product['commissionset'] == 3) {
//提成是积分
					$commissiondata = json_decode($product['commissiondata3'], true);
					if ($commissiondata) {
						$commission = $commissiondata[$userlevel['id']]['commission1'];
					}
					$product['commission_desc'] = t('积分');
					//}elseif($product['commissionset']==4 && $product['lvprice']==1){//按价格差
					//	$lvprice_data = json_decode($product['lvprice_data'],true);
					//    $commission = array_shift($lvprice_data) - $product['sell_price_origin'];
					//	if($commission < 0) $commission = 0;
				} elseif ($product['commissionset'] == 0) {
//按会员等级
					//fxjiesuantype 0按商品价格,1按成交价格,2按销售利润
					if ($userlevel['commissiontype'] == 1) {
						//固定金额按单
						$commission = $userlevel['commission1'];
					} else {
						$commission = $userlevel['commission1'] * ($product['sell_price_origin'] - ($sysset['fxjiesuantype'] == 2 ? $product['cost_price'] : 0)) * 0.01;
					}
				}
				if ($product['commissionset4'] == 1 && $product['lvprice'] == 1) {
					//极差分销
					$lvprice_data = json_decode($product['lvprice_data'], true);
					$commission += array_shift($lvprice_data) - $product['sell_price'];
					if ($commission < 0) {
						$commission = 0;
					}

				}
			}
		}
		$product['commission'] = round($commission * 100) / 100;
		unset($product['cost_price']);

		//节省金额
		$product['jiesheng_money'] = round($oldproduct['sell_price'] - $product['sell_price'], 2);
		if ($product['jiesheng_money'] <= 0) {
			$product['jiesheng_money'] = round($product['market_price'] - $product['sell_price'], 2);
		}

		if ($product['jiesheng_money'] <= 0) {
			$product['jiesheng_money'] = 0;
		}

		//升级到下一等级预计节省多少钱
		if ($shopset['show_lvupsavemoney'] == 1 && $this->member) {
			$upsavemoney = 0;
			$nextlevel = Db::name('member_level')->where('aid', aid)->where('sort', '>', $userlevel['sort'])->order('sort,id')->find();
			if ($nextlevel) {
				$sell_price = $oldproduct['sell_price'];
				if ($product['lvprice'] == 0 && $product['no_discount'] == 0 && $userlevel['discount'] > 0 && $userlevel['discount'] < 10) {
					$this_sell_price = $sell_price * $userlevel['discount'] * 0.1;
				} else {
					if ($product['lvprice'] == 1) {
						$lvprice_data = json_decode($oldproduct['lvprice_data'], true);
						if ($lvprice_data && isset($lvprice_data[$this->member['levelid']])) {
							$this_sell_price = $lvprice_data[$this->member['levelid']];
						}
					} else {
						$this_sell_price = $sell_price;
					}
				}
				if ($product['lvprice'] == 0 && $product['no_discount'] == 0 && $nextlevel['discount'] > 0 && $nextlevel['discount'] < 10) {
					$next_sell_price = $sell_price * $nextlevel['discount'] * 0.1;
				} else {
					if ($product['lvprice'] == 1) {
						$lvprice_data = json_decode($oldproduct['lvprice_data'], true);
						if ($lvprice_data && isset($lvprice_data[$nextlevel['id']])) {
							$next_sell_price = $lvprice_data[$nextlevel['id']];
						}
					} else {
						$next_sell_price = $sell_price;
					}
				}
				if ($this_sell_price > $next_sell_price) {
					$upsavemoney = round($this_sell_price - $next_sell_price, 2);
				}
			}
			$product['upsavemoney'] = $upsavemoney;
			$product['nextlevelname'] = $nextlevel ? $nextlevel['name'] : '';
		} else {
			$product['upsavemoney'] = 0;
		}

		if ($product['balance'] > 0) {
			$product['advance_price'] = round($product['sell_price'] * (1 - $product['balance'] * 0.01), 2);
			$product['balance_price'] = round($product['sell_price'] * $product['balance'] * 0.01, 2);
		}

		if (getcustom('buy_selectmember') && $this->member) {
			if ($userlevel['can_buyselect'] == 1) {
				$product['buyselect_commission'] = round($userlevel['buyselect_commission'] * $product['sell_price'] * 0.01, 2);
			}
		}

		if ($product['bid'] != 0) {
			$business = Db::name('business')->where('aid', aid)->where('id', $product['bid'])->field('id,name,logo,desc,tel,address,sales,kfurl')->find();
		} else {
			$business = $sysset;
		}
		$product['detail'] = \app\common\System::initpagecontent($product['detail'], aid, mid, platform);
		$product['comment_starnum'] = floor($product['comment_score']);

		if ($this->member && $product['lvprice'] == 0 && $product['no_discount'] == 0 && $userlevel['discount'] > 0 && $userlevel['discount'] < 10) {
			$product['discount_tips'] = ' ' . $userlevel['name'] . ' 享' . floatval($userlevel['discount']) . '折优惠';
		} else {
			$product['discount_tips'] = '';
		}

		$sysset['showgzts'] = false;
		//关注提示
		if (platform == 'mp') {
			$sysset['gzts'] = explode(',', $sysset['gzts']);
			if (in_array('2', $sysset['gzts']) && $this->member['subscribe'] == 0) {
				$appinfo = \app\common\System::appinfo(aid, 'mp');
				$sysset['qrcode'] = $appinfo['qrcode'];
				$sysset['gzhname'] = $appinfo['nickname'];
				$sysset['showgzts'] = true;
			}
		}
		//订单播报
		$bboglist = [];
		$sysset['ddbb'] = explode(',', $sysset['ddbb']);
		if (in_array('2', $sysset['ddbb'])) {
			$bboglist = Db::name('shop_order_goods')
				->field('mid,name,createtime,proid')
				->where('aid', aid)
				->where('proid', $product['id'])
				->where('status', 'in', '0,1,2,3')
				->where('createtime', '>', time() - 86400 * 10)
				->order('createtime desc')->limit(10)->select()->toArray();
			if (!$bboglist) {
				$bboglist = [];
			}

			foreach ($bboglist as $k => $og) {
				$ogmember = Db::name('member')->where('id', $og['mid'])->find();
				if (!$ogmember) {
					unset($bboglist[$k]);
					continue;
				} else {
					$bboglist[$k]['nickname'] = $ogmember['nickname'];
					$bboglist[$k]['headimg'] = $ogmember['headimg'];
				}
				if (time() - $og['createtime'] < 60 * 5) {
					$bboglist[$k]['showtime'] = '刚刚';
				} elseif (date('Ymd') == date('Ymd', $og['createtime'])) {
					if ($og['createtime'] + 3600 > time()) {
						$bboglist[$k]['showtime'] = floor((time() - $og['createtime']) / 60) . '分钟前';
					} else {
						$bboglist[$k]['showtime'] = floor((time() - $og['createtime']) / 3600) . '小时前';
					}
				} elseif (time() - $og['createtime'] < 86400) {
					$bboglist[$k]['showtime'] = '昨天';
				} elseif (time() - $og['createtime'] < 2 * 86400) {
					$bboglist[$k]['showtime'] = '前天';
				} else {
					$bboglist[$k]['showtime'] = '三天前';
				}
			}
		}

		//促销活动
		$cuxiaolist = Db::name('cuxiao')
			->where('aid', aid)
			->where('bid', $product['bid'])
			->where('starttime', '<', time())
			->where('endtime', '>', time())
			->order('sort desc')->select()->toArray();
		$newcxlist = [];
		foreach ($cuxiaolist as $k => $v) {
			$gettj = explode(',', $v['gettj']);
			if (!in_array('-1', $gettj) && !in_array($this->member['levelid'], $gettj)) {
				//不是所有人
				continue;
			}
			if ($v['fwtype'] == 2) {
//指定商品可用
				$productids = explode(',', $v['productids']);
				if (!in_array($product['id'], $productids)) {
					continue;
				}
			}
			if ($v['fwtype'] == 1) {
//指定类目可用
				$categoryids = explode(',', $v['categoryids']);
				$cids = explode(',', $product['cid']);
				$clist = Db::name('shop_category')->where('pid', 'in', $categoryids)->select()->toArray();
				foreach ($clist as $kc => $vc) {
					$categoryids[] = $vc['id'];
					$cate2 = Db::name('shop_category')->where('pid', $vc['id'])->find();
					$categoryids[] = $cate2['id'];
				}
				if (!array_intersect($cids, $categoryids)) {
					continue;
				}
			}
			$newcxlist[] = $v;
		}
		//优惠券isgive=2 仅转赠的卡券，不可自己使用
		$couponlist = Db::name('coupon')->where('aid', aid)->where('bid', $product['bid'])->where('isgive', '<>', 2)->where('tolist', 1)->where('type', 'in', '1,4,10')->where("unix_timestamp(starttime)<=" . time() . " and unix_timestamp(endtime)>=" . time())->order('sort desc,id desc')->select()->toArray();
		$newcplist = [];
		foreach ($couponlist as $k => $v) {
			$showtj = explode(',', $v['showtj']);
			if (!in_array('-1', $showtj) && !in_array($this->member['levelid'], $showtj)) {
				//不是所有人
				continue;
			}
			//0全场通用,1指定类目,2指定商品
			if (!in_array($v['fwtype'], [0, 1, 2])) {
				continue;
			}
			if ($v['fwtype'] == 2) {
//指定商品可用
				$productids = explode(',', $v['productids']);
				if (!in_array($product['id'], $productids)) {
					continue;
				}
			}
			if ($v['fwtype'] == 1) {
//指定类目可用
				$categoryids = explode(',', $v['categoryids']);
				$cids = explode(',', $product['cid']);
				$clist = Db::name('shop_category')->where('pid', 'in', $categoryids)->select()->toArray();
				foreach ($clist as $kc => $vc) {
					$categoryids[] = $vc['id'];
					$cate2 = Db::name('shop_category')->where('pid', $vc['id'])->find();
					$categoryids[] = $cate2['id'];
				}
				if (!array_intersect($cids, $categoryids)) {
					continue;
				}
			}
			$haveget = Db::name('coupon_record')->where('aid', aid)->where('mid', mid)->where('couponid', $v['id'])->count();
			$v['haveget'] = $haveget;
			//$v['starttime'] = date('m-d H:i',strtotime($v['starttime']));
			//$v['endtime'] = date('m-d H:i',strtotime($v['endtime']));
			if ($v['yxqtype'] == 1) {
				$yxqtime = explode(' ~ ', $v['yxqtime']);
				$v['yxqdate'] = strtotime($yxqtime[1]);
			} elseif ($v['yxqtype'] == 2) {
				$v['yxqdate'] = time() + 86400 * $v['yxqdate'];
			} elseif ($v['yxqtype'] == 3) {
				//次日起计算有效期
				$v['yxqdate'] = strtotime(date('Y-m-d')) + 86400 * ($v['yxqdate'] + 1) - 1;
			}
			if ($v['bid'] > 0) {
				$v['bname'] = Db::name('business')->where('aid', aid)->where('id', $v['bid'])->value('name');
			}
			$newcplist[] = $v;
		}

		//商品服务
		if ($product['fwid']) {
			$fuwulist = Db::name('shop_fuwu')->where('aid', aid)->where('status', 1)->where('id', 'in', $product['fwid'])->order('sort desc,id')->select()->toArray();
		} else {
			$fuwulist = [];
		}

		$minprice = 999999999999999;
		$maxprice = 0;
		$gglist = Db::name('shop_guige')->where('proid', $product['id'])->select()->toArray();
		if ($product['lvprice'] == 1) {
			$gglist = $this->formatgglist($gglist, $product['bid']);
		}

		foreach ($gglist as $k => $v) {
			if ($v['sell_price'] < $minprice) {
				$minprice = $v['sell_price'];
			}
			if ($v['sell_price'] > $maxprice) {
				$maxprice = $v['sell_price'];
			}
		}
		$product['min_price'] = round($minprice, 2);
		$product['max_price'] = round($maxprice, 2);

		if (getcustom('shoptongji3')) {
			Db::name('shop_product')->where('id', $product['id'])->inc('viewnum')->update();
		}

		$tjdatalist = [];
		if ($product['show_recommend'] == 1) {
			$tjwhere = [];
			$tjwhere[] = ['aid', '=', aid];
			$tjwhere[] = ['status', '=', 1];
			$tjwhere[] = ['ischecked', '=', 1];
			$where2 = "find_in_set('-1',showtj)";
			if ($this->member) {
				$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
				if ($this->member['subscribe'] == 1) {
					$where2 .= " or find_in_set('0',showtj)";
				}
			}
			$tjwhere[] = Db::raw($where2);

			if ($product['bid']) {
				$tjwhere[] = ['bid', '=', $product['bid']];
			} else {
				$business_sysset = Db::name('business_sysset')->where('aid', aid)->find();
				if (!$business_sysset || $business_sysset['status'] == 0 || $business_sysset['product_isshow'] == 0) {
					$tjwhere[] = ['bid', '=', 0];
				}
			}
			$tjdatalist = Db::name('shop_product')->field("id,pic,name,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,price_type")->where($tjwhere)->limit(8)->order(Db::raw('rand()'))->select()->toArray();
			if (!$tjdatalist) {
				$tjdatalist = array();
			}

			$tjdatalist = $this->formatprolist($tjdatalist);
		} elseif ($product['show_recommend'] == 2) {
			$tjdatalist = Db::name('shop_product')->where('aid', aid)->where('id', 'in', $product['recommend_productids'])->order(Db::raw('field(id,' . $product['recommend_productids'] . ')'))->select()->toArray();
		}

		$xcx_scheme = false;
		if (getcustom('xcx_scheme')) {
			$xcx_scheme = true;
		}

		$rdata = [];
		$rdata['status'] = 1;
		$rdata['title'] = $product['name'];
		$rdata['shopset'] = $shopset;
		$rdata['sysset'] = $sysset;
		$rdata['isfavorite'] = $isfavorite;
		$rdata['product'] = $product;
		$rdata['business'] = $business;
		$rdata['commentlist'] = $commentlist;
		$rdata['commentcount'] = $commentcount;
		$rdata['cartnum'] = Db::name('shop_cart')->where('aid', aid)->where('mid', mid)->sum('num');
		$rdata['bboglist'] = $bboglist;
		$rdata['cuxiaolist'] = $newcxlist;
		$rdata['couponlist'] = $newcplist;
		$rdata['fuwulist'] = $fuwulist;
		$rdata['tjdatalist'] = $tjdatalist;
		$rdata['showjiesheng'] = 0;
		if (getcustom('showjiesheng') && $shopset['showcommission'] == 1 && $product['commission'] > 0) {
			$rdata['showjiesheng'] = 1;
		}
		$rdata['showtoptabbar'] = 0;
		if (getcustom('ngmm')) {
			$rdata['showtoptabbar'] = 1;
			$rdata['shopset'] = Db::name('shop_sysset')->where('aid', aid)->field('showjd,comment,showcommission,hide_sales,hide_stock,show_lvupsavemoney,detail_guangao1,detail_guangao1_t,detail_guangao2,detail_guangao2_t')->find();
		}

		$rdata['xcx_scheme'] = $xcx_scheme;
		return $this->json($rdata);
	}
	//获取商品详情
	public function getproductdetail() {
		$proid = input('param.id/d');
		$where = [];
		$where[] = ['aid', '=', aid];
		$where[] = ['id', '=', $proid];
		$field = "bid,id,pic,name,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,guigedata,status,ischecked,freighttype,start_time,end_time,start_hours,end_hours,balance,limit_start,perlimitdan,commissionset,commissiondata1,commissiondata2,commissiondata3,commissionset4,price_type";
		if (getcustom('plug_tengrui')) {
			$field .= ',house_status,group_status,group_ids,is_rzh,relation_type';
		}
		if (getcustom('shop_other_infor')) {
			$field .= ',xunjia_text';
		}
		if (getcustom('product_glass')) {
			$field .= ',product_type';
		}
		$product = Db::name('shop_product')->field($field)->where($where)->find();
		if (!$product) {
			return $this->json(['status' => 0, 'msg' => '商品不存在']);
		}
		$product = $this->formatproduct($product);
		if ($product['status'] == 0) {
			return $this->json(['status' => 0, 'msg' => '商品已下架']);
		}

		if ($product['ischecked'] != 1) {
			return $this->json(['status' => 0, 'msg' => '商品未审核']);
		}

		if ($product['status'] == 2 && (strtotime($product['start_time']) > time() || strtotime($product['end_time']) < time())) {
			return $this->json(['status' => 0, 'msg' => '商品未上架']);
		}
		if ($product['status'] == 3) {
			$start_time = strtotime(date('Y-m-d ' . $product['start_hours']));
			$end_time = strtotime(date('Y-m-d ' . $product['end_hours']));
			if (($start_time < $end_time && ($start_time > time() || $end_time < time())) || ($start_time >= $end_time && ($start_time > time() && $end_time < time()))) {
				return $this->json(['status' => 0, 'msg' => '商品未上架']);
			}
		}

		if ($product['status'] == 2 || $product['status'] == 3) {
			$product['status'] = 1;
		}

		$gglist = Db::name('shop_guige')->where('proid', $product['id'])->select()->toArray();
		if ($product['lvprice'] == 1) {
			$gglist = $this->formatgglist($gglist, $product['bid']);
		}

		$sysset = Db::name('admin_set')->where('aid', aid)->field('name,logo,desc,fxjiesuantype,tel,kfurl,gzts,ddbb')->find();
		$shopset = Db::name('shop_sysset')->where('aid', aid)->field('showjd,comment,showcommission,hide_sales,hide_stock')->find();

		$guigelist = array();
		foreach ($gglist as $k => $v) {
			if ($product['balance'] > 0) {
				$v['advance_price'] = round($v['sell_price'] * (1 - $product['balance'] * 0.01), 2);
				$v['balance_price'] = round($v['sell_price'] * $product['balance'] * 0.01, 2);
			} else {
				$v['balance_price'] = 0;
			}
			//预计佣金
			$commission = 0;
			$v['commission_desc'] = '元';
			//计算佣金
			if ($this->member && $shopset['showcommission'] == 1 && $product['commissionset'] != -1) {
				$userlevel = Db::name('member_level')->where('aid', aid)->where('id', $this->member['levelid'])->find();
				if ($userlevel['can_agent'] != 0) {
					if ($product['commissionset'] == 1) {
//按比例
						$commissiondata = json_decode($product['commissiondata1'], true);
						if ($commissiondata) {
							$commission = $commissiondata[$userlevel['id']]['commission1'] * ($v['sell_price'] - ($sysset['fxjiesuantype'] == 2 ? $v['cost_price'] : 0)) * 0.01;
						}
					} elseif ($product['commissionset'] == 2) {
//按固定金额
						$commissiondata = json_decode($product['commissiondata2'], true);
						if ($commissiondata) {
							$commission = $commissiondata[$userlevel['id']]['commission1'];
						}
					} elseif ($product['commissionset'] == 3) {
//提成是积分
						$commissiondata = json_decode($product['commissiondata3'], true);
						if ($commissiondata) {
							$commission = $commissiondata[$userlevel['id']]['commission1'];
						}
						$v['commission_desc'] = t('积分');
						//}elseif($product['commissionset']==4 && $product['lvprice']==1){//按价格差
						//    $lvprice_data = json_decode($v['lvprice_data'],true);
						//    $commission = array_shift($lvprice_data) - $v['sell_price'];
						//    if($commission < 0) $commission = 0;
					} elseif ($product['commissionset'] == 0) {
//按会员等级
						//fxjiesuantype 0按商品价格,1按成交价格,2按销售利润
						if ($userlevel['commissiontype'] == 1) {
							//固定金额按单
							$commission = $userlevel['commission1'];
						} else {
							$commission = $userlevel['commission1'] * ($v['sell_price'] - ($sysset['fxjiesuantype'] == 2 ? $v['cost_price'] : 0)) * 0.01;
						}
					}
					if ($product['commissionset4'] == 1 && $product['lvprice'] == 1) {
						//极差分销
						$lvprice_data = json_decode($v['lvprice_data'], true);
						$commission += array_shift($lvprice_data) - $v['sell_price'];
						if ($commission < 0) {
							$commission = 0;
						}

					}
				}
			}
			$v['commission'] = round($commission * 100) / 100;

			$guigelist[$v['ks']] = $v;
		}
		$guigedata = json_decode($product['guigedata'], true);
		$ggselected = [];
		foreach ($guigedata as $v) {
			$ggselected[] = 0;
		}
		$ks = implode(',', $ggselected);
		return $this->json(['status' => 1, 'product' => $product, 'guigelist' => $guigelist, 'guigedata' => $guigedata, 'ggselected' => $ggselected, 'ks' => $ks, 'shopset' => $shopset]);
	}
	//商品评价
	public function commentlist() {
		$proid = input('param.proid/d');
		$pagenum = input('post.pagenum');
		if (!$pagenum) {
			$pagenum = 1;
		}

		$pernum = 20;
		$where = [];
		$where[] = ['aid', '=', aid];
		if ($proid != 0) {
			$where[] = ['proid', '=', $proid];
		} else {
			$where[] = ['bid', '=', 0];
		}
		$where[] = ['status', '=', 1];
		$datalist = Db::name('shop_comment')->where($where)->page($pagenum, $pernum)->order('id desc')->select()->toArray();
		if (!$datalist) {
			$datalist = [];
		}

		foreach ($datalist as $k => $pl) {
			$datalist[$k]['createtime'] = date('Y-m-d H:i', $pl['createtime']);
			if ($datalist[$k]['content_pic']) {
				$datalist[$k]['content_pic'] = explode(',', $datalist[$k]['content_pic']);
			}

		}
		if (request()->isPost()) {
			return $this->json(['status' => 1, 'data' => $datalist]);
		}
		$rdata = [];
		$rdata['datalist'] = $datalist;
		return $this->json($rdata);
	}
	//商品海报
	function getposter() {
		$this->checklogin();
		$post = input('post.');
		$platform = platform;
		$page = '/pages/shop/product';
		$scene = 'id_' . $post['proid'] . '-pid_' . $this->member['id'];
		//if($platform == 'mp' || $platform == 'h5' || $platform == 'app'){
		//	$page = PRE_URL .'/h5/'.aid.'.html#'. $page;
		//}
		$posterset = Db::name('admin_set_poster')->where('aid', aid)->where('type', 'product')->where('platform', $platform)->order('id')->find();

//		$posterdata = Db::name('member_poster')->where('aid',aid)->where('mid',mid)->where('scene',$scene)->where('type','product')->where('posterid',$posterset['id'])->find();
		//关闭缓存
		if (true || !$posterdata) {
			$product = Db::name('shop_product')->where('id', $post['proid'])->find();
			//$product = $this->formatproduct($product);
			$sysset = Db::name('admin_set')->where('aid', aid)->find();
			$textReplaceArr = [
				'[头像]' => $this->member['headimg'],
				'[昵称]' => $this->member['nickname'],
				'[姓名]' => $this->member['realname'],
				'[手机号]' => $this->member['mobile'],
				'[商城名称]' => $sysset['name'],
				'[商品名称]' => $product['name'],
				'[商品销售价]' => $product['sell_price'],
				'[商品市场价]' => $product['market_price'],
				'[商品图片]' => $product['pic'],
			];

			$poster = $this->_getposter(aid, $platform, $posterset['content'], $page, $scene, $textReplaceArr);
			$posterdata = [];
			$posterdata['aid'] = aid;
			$posterdata['mid'] = $this->member['id'];
			$posterdata['scene'] = $scene;
			$posterdata['page'] = $page;
			$posterdata['type'] = 'product';
			$posterdata['poster'] = $poster;
			$posterdata['posterid'] = $posterset['id'];
			$posterdata['createtime'] = time();
//			Db::name('member_poster')->insert($posterdata);
		}
		return $this->json(['status' => 1, 'poster' => $posterdata['poster']]);
	}
	//购物车
	public function cart() {
		//$this->checklogin();
		$gwcdata = [];
		$field = 'id,bid,proid,ggid,num';
		if (getcustom('product_glass')) {
			$field .= ',glass_record_id';
		}
		if (input('param.bid')) {
			$cartlist = Db::name('shop_cart')->field($field)->where('aid', aid)->where('bid', input('param.bid'))->where('mid', mid)->order('createtime desc')->select()->toArray();
		} else {
			$cartlist = Db::name('shop_cart')->field($field)->where('aid', aid)->where('mid', mid)->order('createtime desc')->select()->toArray();
		}
		if (!$cartlist) {
			$cartlist = [];
		}

		$newcartlist = [];
		foreach ($cartlist as $k => $gwc) {
			if ($newcartlist[$gwc['bid']]) {
				$newcartlist[$gwc['bid']][] = $gwc;
			} else {
				$newcartlist[$gwc['bid']] = [$gwc];
			}
		}
		foreach ($newcartlist as $bid => $gwclist) {
			if ($bid == 0) {
				$business = Db::name('admin_set')->where('aid', aid)->field('id,name,logo,tel')->find();
			} else {
				$business = Db::name('business')->where('aid', aid)->where('id', $bid)->field('id,name,logo,tel')->find();
			}
			$prolist = [];
			foreach ($gwclist as $gwc) {
				$product = Db::name('shop_product')->where('aid', aid)->where('status', '<>', 0)->where('id', $gwc['proid'])->find();
				if (!$product) {
					Db::name('shop_cart')->where('aid', aid)->where('proid', $gwc['proid'])->delete();
					continue;
				}
				$guige = Db::name('shop_guige')->where('id', $gwc['ggid'])->find();
				if (!$guige) {
					Db::name('shop_cart')->where('aid', aid)->where('ggid', $gwc['ggid'])->delete();
					continue;
				}
				if ($product['lvprice'] == 1) {
					$guige = $this->formatguige($guige, $product['bid']);
				}
				if ($product['perlimitdan'] > 0 && $gwc['num'] > $product['perlimitdan']) {
					$gwc['num'] = $product['perlimitdan'];
					Db::name('shop_cart')->where('aid', aid)->where('id', $gwc['id'])->update(['num' => $gwc['num']]);
				}
				$tmpitem = ['id' => $gwc['id'], 'checked' => true, 'product' => $product, 'guige' => $guige, 'num' => $gwc['num']];
				if (getcustom('product_glass')) {
					$glassrecord = '';
					if ($gwc['glass_record_id']) {
						$glassrecord = Db::name('glass_record')->where('aid', aid)->where('mid', $this->mid)->where('id', $gwc['glass_record_id'])->find();
					}
					$tmpitem['glassrecord'] = $glassrecord ?? '';
				}
				$prolist[] = $tmpitem;
			}
			$newcartlist[$bid] = ['bid' => $bid, 'checked' => true, 'business' => $business, 'prolist' => $prolist];
		}

		$shopset = Db::name('shop_sysset')->where('aid', aid)->field('gwctj')->find();
		if ($shopset['gwctj']) {
			$tjwhere = [];
			$tjwhere[] = ['aid', '=', aid];
			$tjwhere[] = ['status', '=', 1];
			$tjwhere[] = ['ischecked', '=', 1];
			if (isdouyin == 1) {
				$tjwhere[] = ['douyin_product_id', '<>', ''];
			} else {
				$tjwhere[] = ['douyin_product_id', '=', ''];
			}
			$where2 = "find_in_set('-1',showtj)";
			if ($this->member) {
				$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
				if ($this->member['subscribe'] == 1) {
					$where2 .= " or find_in_set('0',showtj)";
				}
			}
			$tjwhere[] = Db::raw($where2);

			if (input('param.bid')) {
				$tjwhere[] = ['bid', '=', input('param.bid/d')];
			} else {
				$business_sysset = Db::name('business_sysset')->where('aid', aid)->find();
				if (!$business_sysset || $business_sysset['status'] == 0 || $business_sysset['product_isshow'] == 0) {
					$tjwhere[] = ['bid', '=', 0];
				}
			}
			$tjdatalist = Db::name('shop_product')->field("id,pic,name,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,price_type")->where($tjwhere)->limit(12)->order(Db::raw('rand()'))->select()->toArray();
			if (!$tjdatalist) {
				$tjdatalist = array();
			}

			$tjdatalist = $this->formatprolist($tjdatalist);
		} else {
			$tjdatalist = [];
		}
		$rdata = [];
		$rdata['cartlist'] = array_values($newcartlist);
		if (getcustom('xixie')) {
			if (!input('param.bid')) {
				$xx_list = Db::name('xixie_cart')->where('mid', mid)->where('aid', aid)->field('id,bid,proid,num')->order('createtime desc')->select()->toArray();

				$xx_newlist = [];
				foreach ($xx_list as $xcv) {
					if ($xx_newlist[$xcv['bid']]) {
						$xx_newlist[$xcv['bid']][] = $xcv;
					} else {
						$xx_newlist[$xcv['bid']] = [$xcv];
					}
				}
				foreach ($xx_newlist as $xx_bid => $xnv) {
					$business = Db::name('admin_set')->where('aid', aid)->field('id,name,logo,tel')->find();
					if ($business) {
						$business['name'] .= ' 洗鞋';
					}
					$prolist = [];
					foreach ($xnv as $xv) {
						$product = Db::name('xixie_product')->where('id', $xv['proid'])->where('aid', aid)->where('status', '<>', 0)->find();
						if (!$product) {
							Db::name('xixie_cart')->where('proid', $xv['proid'])->where('aid', aid)->delete();
							continue;
						}
						if ($this->member['is_vip'] && $product['vip_price'] > 0) {
							$product['sell_price'] = $product['vip_price'];
						}
						if ($product['buymax'] > 0 && $xv['num'] > $product['buymax']) {
							$xv['num'] = $product['buymax'];
							Db::name('xixie_cart')->where('aid', aid)->where('id', $xv['id'])->update(['num' => $xv['num']]);
						}
						$prolist[] = ['id' => $xv['id'], 'checked' => true, 'product' => $product, 'num' => $xv['num'], 'type' => 2];
					}
					$xx_newlist[$xx_bid] = ['bid' => $xx_bid, 'checked' => true, 'business' => $business, 'prolist' => $prolist];
				}
			} else {
				$xx_newlist = '';
			}
			$rdata['xixie'] = true;
			$rdata['xixie_cartlist'] = $xx_newlist ? array_values($xx_newlist) : '';
		}
		$rdata['tjdatalist'] = $tjdatalist;
		return $this->json($rdata);
	}
	public function addcart() {
		$this->checklogin();
		$post = input('post.');
		$oldnum = 0;
		$num = intval($post['num']);

		$product = Db::name('shop_product')->where('aid', aid)->where('status', '<>', 0)->where('ischecked', 1)->where('id', $post['proid'])->find();
		if (!$product) {
			return $this->json(['status' => 0, 'msg' => '产品不存在或已下架']);
		}

		if ($product['freighttype'] == 3 || $product['freighttype'] == 4) {
			return $this->json(['status' => 0, 'msg' => '虚拟商品不能加入购物车']);
		}

		if (!$post['ggid']) {
			if ($num > 0) {
				$post['ggid'] = Db::name('shop_guige')->where('proid', $post['proid'])->value('id');
			} else {
				$post['ggid'] = Db::name('shop_cart')->where('aid', aid)->where('mid', mid)->where('proid', $post['proid'])->order('id desc')->value('ggid');
			}
		}

		$gwc = Db::name('shop_cart')->where('aid', aid)->where('mid', mid)->where('proid', $post['proid'])->where('ggid', $post['ggid'])->find();
		if ($gwc) {
			$oldnum = $gwc['num'];
		}

		if ($num > 0 && $product['limit_start'] > 0 && $oldnum + $num < $product['limit_start']) {
			$num = $product['limit_start'];
		}
		if ($num > 0 && $product['perlimitdan'] > 0 && $oldnum + $num > $product['perlimitdan']) {
			return $this->json(['status' => 0, 'msg' => '每单限购' . $product['perlimitdan'] . '件']);
			$num = $product['perlimitdan'];
		}
		if (getcustom('plug_tengrui')) {
			//判断是否是否符合会员认证、会员关系、一户、用户组
			$tr_check = new \app\common\TengRuiCheck();
			$check_product = $tr_check->check_product($this->member, $product);
			if ($check_product && $check_product['status'] == 0) {
				return $this->json(['status' => $check_product['status'], 'msg' => $check_product['msg']]);
			}
			$tr_roomId = $check_product['tr_roomId'];
		}
		if ($oldnum + $num <= 0 || ($product['limit_start'] > 0 && $oldnum + $num < $product['limit_start'])) {
			Db::name('shop_cart')->where('aid', aid)->where('mid', mid)->where('proid', $post['proid'])->where('ggid', $post['ggid'])->delete();
			$cartnum = Db::name('shop_cart')->where('aid', aid)->where('mid', mid)->sum('num');
			return $this->json(['status' => 1, 'msg' => '移除成功', 'cartnum' => $cartnum]);
		}
		if ($gwc) {
			$updata['createtime'] = time();
			if (getcustom('product_glass')) {
				$data['glass_record_id'] = $post['glass_record_id'];
			}
			Db::name('shop_cart')->where('aid', aid)->where('mid', mid)->where('proid', $post['proid'])->where('ggid', $post['ggid'])->inc('num', $num)->update($updata);
		} else {
			$data = [];
			$data['aid'] = aid;
			$data['bid'] = $product['bid'];
			$data['mid'] = mid;
			$data['ggid'] = $post['ggid'];
			$data['createtime'] = time();
			$data['proid'] = $post['proid'];
			$data['num'] = $num;
			if (getcustom('product_glass')) {
				$data['glass_record_id'] = $post['glass_record_id'];
			}
			Db::name('shop_cart')->insert($data);
		}
		$cartnum = Db::name('shop_cart')->where('aid', aid)->where('mid', mid)->sum('num');
		return $this->json(['status' => 1, 'msg' => '加入购物车成功', 'cartnum' => $cartnum]);
	}
	public function cartChangenum() {
		$this->checklogin();
		$id = input('post.id/d');
		$num = input('post.num/d');
		$type = input('post.type') ? input('post.type') : '';
		if ($num < 1) {
			$num = 1;
		}

		if (!$type) {
			$cart = Db::name('shop_cart')->where('aid', aid)->where('id', $id)->where('mid', mid)->find();
			$product = Db::name('shop_product')->where('aid', aid)->where('id', $cart['proid'])->find();
			$guige = Db::name('shop_guige')->where('aid', aid)->where('id', $cart['ggid'])->find();
			if ($guige['limit_start'] > 0 && $num < $guige['limit_start']) {
				return $this->json(['status' => 0, 'msg' => '该商品规格' . $guige['limit_start'] . '件起售']);
			}
			if ($product['limit_start'] > 0 && $num < $product['limit_start']) {
				return $this->json(['status' => 0, 'msg' => '该商品' . $product['limit_start'] . '件起售']);
			}
			if ($product['perlimitdan'] > 0 && $num > $product['perlimitdan']) {
				return $this->json(['status' => 0, 'msg' => '该商品每单限购' . $product['perlimitdan'] . '件']);
			}
		}
		if (getcustom('xixie')) {
			if ($type == 2) {
				$cart = Db::name('xixie_cart')->where('id', $id)->where('mid', mid)->where('aid', aid)->find();
				$product = Db::name('xixie_product')->where('id', $cart['proid'])->where('aid', aid)->find();
				if ($product['buymax'] > 0 && $num > $product['buymax']) {
					return $this->json(['status' => 0, 'msg' => '该商品每单限购' . $product['buymax'] . '件']);
				}
				$up = Db::name('xixie_cart')->where('id', $id)->where('mid', mid)->update(['num' => $num]);
				if ($up) {
					return $this->json(['status' => 1, 'msg' => '修改成功']);
				} else {
					return $this->json(['status' => 0, 'msg' => '修改成功']);
				}
			}
		}
		Db::name('shop_cart')->where('id', $id)->where('mid', mid)->update(['num' => $num]);
		return $this->json(['status' => 1, 'msg' => '修改成功']);
	}
	public function cartdelete() {
		$this->checklogin();
		$id = input('post.id/d');
		$type = input('post.type') ? input('post.type') : '';
		if (!$type) {
			if (!$id) {
				$bid = input('post.bid/d');
				Db::name('shop_cart')->where('bid', $bid)->where('mid', mid)->delete();
				return $this->json(['status' => 1, 'msg' => '删除成功']);
			}
		}
		if (getcustom('xixie')) {
			if ($type == 2) {
				if ($id) {
					//删除购物车
					$del = Db::name('xixie_cart')->where('id', $id)->where('mid', mid)->delete();
					if ($del) {
						return $this->json(['status' => 1, 'msg' => '删除成功']);
					} else {
						return $this->json(['status' => 0, 'msg' => '删除失败']);
					}
				} else {
					//删除购物车
					$del = Db::name('xixie_cart')->where('bid', 0)->where('mid', mid)->delete();
					if ($del) {
						return $this->json(['status' => 1, 'msg' => '删除成功']);
					} else {
						return $this->json(['status' => 0, 'msg' => '删除失败']);
					}
					return $this->json(['status' => 0, 'msg' => '删除失败']);
				}
			}
		}
		Db::name('shop_cart')->where('id', $id)->where('mid', mid)->delete();
		return $this->json(['status' => 1, 'msg' => '删除成功']);
	}
	public function cartclear() {
		$bid = input('param.bid');
		if (!$bid) {
			$bid = 0;
		}

		Db::name('shop_cart')->where('aid', aid)->where('bid', $bid)->where('mid', mid)->delete();
		return $this->json(['status' => 1, 'msg' => '删除成功']);
	}
	//快速购买页
	public function fastbuy() {
		$bid = input('param.bid');
		if (!$bid) {
			$bid = 0;
		}

		$cid = input('param.cid');
		if (!$cid) {
			$cid = 0;
		}

		if ($bid > 0) {
			$clist = Db::name('shop_category2')->where('aid', aid)->where('bid', $bid)->where('pid', $cid)->where('status', 1)->order('sort desc,id')->select()->toArray();
		} else {
			$where2 = "find_in_set('-1',showtj)";
			if ($this->member) {
				$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
				if ($this->member['subscribe'] == 1) {
					$where2 .= " or find_in_set('0',showtj)";
				}
			}
			$tjwhere[] = Db::raw($where2);
			$clist = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', $cid)->where('status', 1)->order('sort desc,id')->select()->toArray();
		}
		foreach ($clist as $k => $v) {
			$where = [];
			$where[] = ['aid', '=', aid];
			$where[] = ['bid', '=', $bid];
			//$where[] = ['status','=',1];
			$where[] = ['ischecked', '=', 1];
			$where[] = ['freighttype', '<', 3];
			if (isdouyin == 1) {
				$where[] = ['douyin_product_id', '<>', ''];
			} else {
				$where[] = ['douyin_product_id', '=', ''];
			}
			$nowtime = time();
			$nowhm = date('H:i');
			$where[] = Db::raw("`status`=1 or (`status`=2 and unix_timestamp(start_time)<=$nowtime and unix_timestamp(end_time)>=$nowtime) or (`status`=3 and ((start_hours<end_hours and start_hours<='$nowhm' and end_hours>='$nowhm') or (start_hours>=end_hours and (start_hours<='$nowhm' or end_hours>='$nowhm'))) )");

			//子分类
			if ($bid > 0) {
				$childcid = db('shop_category2')->where(['aid' => aid, 'pid' => $v['id']])->column('id');
				if ($childcid) {
					$child2cid = db('shop_category2')->where(['aid' => aid, 'pid' => ['in', $childcid]])->column('id');
					$cCate = array_merge($childcid, $child2cid, [$v['id']]);
					if ($cCate) {
						$whereCid = [];
						foreach ($cCate as $c2) {
							$whereCid[] = "find_in_set({$c2},cid2)";
						}
						$where[] = Db::raw(implode(' or ', $whereCid));
					}
				} else {
					$where[] = Db::raw("find_in_set(" . $v['id'] . ",cid2)");
				}
			} else {
				$childcid = db('shop_category')->where($tjwhere)->where(['aid' => aid, 'pid' => $v['id']])->column('id');
				if ($childcid) {
					$child2cid = db('shop_category')->where($tjwhere)->where(['aid' => aid, 'pid' => ['in', $childcid]])->column('id');
					$cCate = array_merge($childcid, $child2cid, [$v['id']]);
					if ($cCate) {
						$whereCid = [];
						foreach ($cCate as $c2) {
							$whereCid[] = "find_in_set({$c2},cid)";
						}
						$where[] = Db::raw(implode(' or ', $whereCid));
					}
				} else {
					$where[] = Db::raw("find_in_set(" . $v['id'] . ",cid)");
				}
			}
			//是否可见
			$where2 = "find_in_set('-1',showtj)";
			if ($this->member) {
				$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
				if ($this->member['subscribe'] == 1) {
					$where2 .= " or find_in_set('0',showtj)";
				}
			}
			$where[] = Db::raw($where2);

			$prolist = Db::name('shop_product')->field("pic,id,name,stock,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,guigedata,limit_start,price_type")->where($where)->order('sort desc,id desc')->select()->toArray();
			if (!$prolist) {
				$prolist = [];
			}

			$prolist = $this->formatprolist($prolist);
			if (!$prolist) {
				unset($clist[$k]);
			} else {
				$clist[$k]['prolist'] = $prolist;
			}
		}
		$clist = array_values($clist);

		$list = Db::name('shop_cart')->where('aid', aid)->where('bid', $bid)->where('mid', mid)->order('createtime desc')->select()->toArray();
		$total = 0;
		$totalprice = 0;
		foreach ($list as $k => $v) {
			$product = Db::name('shop_product')->field('pic,bid,id,name,stock,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,guigedata,limit_start')->where('id', $v['proid'])->find();
			if (!$product) {
				unset($list[$k]);
				Db::name('shop_cart')->where('id', $v['id'])->delete();
				continue;
			}
			$product = $this->formatproduct($product);
			$guige = Db::name('shop_guige')->where('id', $v['ggid'])->find();
			if (!$guige) {
				unset($list[$k]);
				Db::name('shop_cart')->where('id', $v['id'])->delete();
				continue;
			}
			if ($product['lvprice'] == 1) {
				$guige = $this->formatguige($guige, $product['bid']);
			}

			$list[$k]['product'] = $product;
			$list[$k]['guige'] = $guige;
			$total += $v['num'];
			$totalprice += $guige['sell_price'] * $v['num'];
		}
		$totalprice = number_format($totalprice, 2, '.', '');
		$cartList = ['list' => $list, 'total' => $total, 'totalprice' => $totalprice];
		$numtotal = [];
		foreach ($clist as $i => $v) {
			foreach ($v['prolist'] as $j => $pro) {
				$numtotal[$pro['id']] = 0;
			}
		}
		foreach ($cartList['list'] as $i => $v) {
			$numtotal[$v['proid']] += $v['num'];
		}

		$rdata = [];
		$rdata['status'] = 1;
		$rdata['data'] = $clist;
		$rdata['cartList'] = $cartList;
		$rdata['numtotal'] = $numtotal;
		return $this->json($rdata);
	}

	//快速购买页2
	public function fastbuy2() {
		$bid = input('param.bid');
		if (!$bid || $bid == 'undefined') {
			$bid = 0;
		}

		if ($bid != 0) {
			$business = Db::name('business')->where('aid', aid)->where('id', $bid)->field('id,name,logo,content,pics,desc,tel,address,sales,start_hours,end_hours,latitude,longitude,comment_num,comment_score,comment_haopercent')->find();
			$business['pic'] = explode(',', $business['pics'])[0];
		} else {
			$business = Db::name('admin_set')->where('aid', aid)->field('id,name,logo,`desc` content,tel,address,latitude,longitude')->find();
			$where = [];
			$where[] = ['aid', '=', aid];
			$where[] = ['proid', '=', $proid];
			$where[] = ['status', '=', 1];
			$comment_num = 0 + Db::name('shop_comment')->where('aid', aid)->where('bid', 0)->where('status', 1)->count();

			$haonum = 0 + Db::name('shop_comment')->where('aid', aid)->where('bid', 0)->where('status', 1)->where('score', '>', 3)->count(); //好评数
			if ($comment_num > 0) {
				$haopercent = round($haonum / $comment_num * 100, 2);
			} else {
				$haopercent = 100;
			}
			$business['comment_num'] = $comment_num;
			$business['comment_haopercent'] = $haopercent;
		}

		$cid = input('param.cid');
		if (!$cid) {
			$cid = 0;
		}

		if ($bid > 0) {
			$clist = Db::name('shop_category2')->where('aid', aid)->where('bid', $bid)->where('pid', $cid)->where('status', 1)->order('sort desc,id')->select()->toArray();
		} else {
			$where2 = "find_in_set('-1',showtj)";
			if ($this->member) {
				$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
				if ($this->member['subscribe'] == 1) {
					$where2 .= " or find_in_set('0',showtj)";
				}
			}
			$tjwhere[] = Db::raw($where2);
			$clist = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', $cid)->where('status', 1)->order('sort desc,id')->select()->toArray();
		}
		foreach ($clist as $k => $v) {
			$where = [];
			$where[] = ['aid', '=', aid];
			$where[] = ['bid', '=', $bid];
			//$where[] = ['status','=',1];
			$where[] = ['ischecked', '=', 1];
			$where[] = ['freighttype', '<', 3];
			if (isdouyin == 1) {
				$where[] = ['douyin_product_id', '<>', ''];
			} else {
				$where[] = ['douyin_product_id', '=', ''];
			}
			$nowtime = time();
			$nowhm = date('H:i');
			$where[] = Db::raw("`status`=1 or (`status`=2 and unix_timestamp(start_time)<=$nowtime and unix_timestamp(end_time)>=$nowtime) or (`status`=3 and ((start_hours<end_hours and start_hours<='$nowhm' and end_hours>='$nowhm') or (start_hours>=end_hours and (start_hours<='$nowhm' or end_hours>='$nowhm'))) )");

			//子分类
			if ($bid > 0) {
				$childcid = db('shop_category2')->where(['aid' => aid, 'pid' => $v['id']])->column('id');
				if ($childcid) {
					$child2cid = db('shop_category2')->where(['aid' => aid, 'pid' => ['in', $childcid]])->column('id');
					$cCate = array_merge($childcid, $child2cid, [$v['id']]);
					if ($cCate) {
						$whereCid = [];
						foreach ($cCate as $c2) {
							$whereCid[] = "find_in_set({$c2},cid2)";
						}
						$where[] = Db::raw(implode(' or ', $whereCid));
					}
				} else {
					$where[] = Db::raw("find_in_set(" . $v['id'] . ",cid2)");
				}
			} else {
				$childcid = db('shop_category')->where($tjwhere)->where(['aid' => aid, 'pid' => $v['id']])->column('id');
				if ($childcid) {
					$child2cid = db('shop_category')->where($tjwhere)->where(['aid' => aid, 'pid' => ['in', $childcid]])->column('id');
					$cCate = array_merge($childcid, $child2cid, [$v['id']]);
					if ($cCate) {
						$whereCid = [];
						foreach ($cCate as $c2) {
							$whereCid[] = "find_in_set({$c2},cid)";
						}
						$where[] = Db::raw(implode(' or ', $whereCid));
					}
				} else {
					$where[] = Db::raw("find_in_set(" . $v['id'] . ",cid)");
				}

			}
			//是否可见
			$where2 = "find_in_set('-1',showtj)";
			if ($this->member) {
				$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
				if ($this->member['subscribe'] == 1) {
					$where2 .= " or find_in_set('0',showtj)";
				}
			}
			$where[] = Db::raw($where2);

			$prolist = Db::name('shop_product')->field("pic,id,name,stock,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,guigedata,price_type")->where($where)->order('sort desc,id desc')->select()->toArray();
			if (!$prolist) {
				$prolist = [];
			}

			$prolist = $this->formatprolist($prolist);
			if (!$prolist) {
				unset($clist[$k]);
			} else {
				foreach ($prolist as $k2 => $v2) {
					$gglist = Db::name('shop_guige')->where('proid', $v2['id'])->select()->toArray();
					$prolist[$k2]['gglist'] = $gglist;
					$prolist[$k2]['ggcount'] = count($gglist);
					if ($v2['limit_start'] == 0) {
						$v2['limit_start'] = 1;
					}

					if ($v2['limit_per'] == 0) {
						$v2['limit_per'] = 999999;
					}

				}
				$clist[$k]['prolist'] = $prolist;
			}
		}
		$clist = array_values($clist);

		$list = Db::name('shop_cart')->where('aid', aid)->where('bid', $bid)->where('mid', mid)->order('createtime desc')->select()->toArray();
		$total = 0;
		$totalprice = 0;
		foreach ($list as $k => $v) {
			$product = Db::name('shop_product')->field('pic,bid,id,name,stock,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,guigedata')->where('id', $v['proid'])->find();
			if (!$product) {
				unset($list[$k]);
				Db::name('shop_cart')->where('id', $v['id'])->delete();
				continue;
			}
			$product = $this->formatproduct($product);
			$guige = Db::name('shop_guige')->where('id', $v['ggid'])->find();
			if (!$guige) {
				unset($list[$k]);
				Db::name('shop_cart')->where('id', $v['id'])->delete();
				continue;
			}
			if ($product['lvprice'] == 1) {
				$guige = $this->formatguige($guige, $product['bid']);
			}

			$list[$k]['product'] = $product;
			$list[$k]['guige'] = $guige;
			$total += $v['num'];
			$totalprice += $guige['sell_price'] * $v['num'];
		}
		$totalprice = number_format($totalprice, 2, '.', '');
		$cartList = ['list' => $list, 'total' => $total, 'totalprice' => $totalprice, 'leftprice' => 0];
		$numtotal = [];
		foreach ($clist as $i => $v) {
			foreach ($v['prolist'] as $j => $pro) {
				$numtotal[$pro['id']] = 0;
			}
		}
		foreach ($cartList['list'] as $i => $v) {
			$numtotal[$v['proid']] += $v['num'];
		}

		if (getcustom('plug_businessqr')) {
			$paylist = Db::name('plug_businessqr_pay')->where('aid', aid)->where('bid', $bid)->where('status', 1)->order('sort desc,id desc')->select()->toArray();
			$menu_list = cache('business_menu_list_' . aid);
			if (empty($menu_list)) {
				$menu_list = [
					[
						'name' => '会员支付',
						'alias' => '',
						'sort' => 1,
						'st' => 3,
					],
					[
						'name' => '商家信息',
						'alias' => '',
						'sort' => 2,
						'st' => 1,
					],
					[
						'name' => '商品',
						'alias' => '',
						'sort' => 3,
						'st' => 0,
					],
				];
			}
		} else {
			$paylist = [];
		}

		$rdata = [];
		$rdata['status'] = 1;
		$rdata['data'] = $clist;
		$rdata['paylist'] = $paylist;
		$rdata['cartList'] = $cartList;
		$rdata['numtotal'] = $numtotal;
		$rdata['business'] = $business;
		$rdata['menuList'] = $menu_list ? $menu_list : [];
		$rdata['bid'] = $bid;
		return $this->json($rdata);
	}
	//获取促销信息
	public function getcuxiaoinfo() {
		$id = input('post.id');
		$multi_promotion = 0;
		if (getcustom('multi_promotion')) {
			$multi_promotion = 1;
		}

		if ($multi_promotion && is_array($id)) {
			$list = Db::name('cuxiao')->whereIn('id', $id)->where('aid', aid)->select();
			if (!$list) {
				return $this->json(['status' => 0, 'msg' => '获取失败']);
			}

			if ($list) {
				foreach ($list as $key => $info) {
					$proinfo[$key] = false;
					$gginfo[$key] = false;
					if (($info['type'] == 2 || $info['type'] == 3) && $info['proid']) {
						$proinfo[$key] = Db::name('shop_product')->field('id,name,pic,sell_price')->where('aid', aid)->where('id', $info['proid'])->find();
						$gginfo[$key] = Db::name('shop_guige')->where('aid', aid)->where('id', $info['ggid'])->find();
					}
				}
			}

			return $this->json(['status' => 1, 'info' => $list, 'product' => $proinfo, 'guige' => $gginfo]);
		} else {
			$info = Db::name('cuxiao')->where('id', $id)->where('aid', aid)->find();
			if (!$info) {
				return $this->json(['status' => 0, 'msg' => '获取失败']);
			}

			$proinfo = false;
			$gginfo = false;
			if (($info['type'] == 2 || $info['type'] == 3) && $info['proid']) {
				$proinfo = Db::name('shop_product')->field('id,name,pic,sell_price')->where('aid', aid)->where('id', $info['proid'])->find();
				$gginfo = Db::name('shop_guige')->where('aid', aid)->where('id', $info['ggid'])->find();
			}
			return $this->json(['status' => 1, 'info' => $info, 'product' => $proinfo, 'guige' => $gginfo]);
		}

	}
	//订单提交页
	public function buy() {
		$this->checklogin();
		$prodata = explode('-', input('param.prodata'));

		$multi_promotion = 0;
		if (getcustom('multi_promotion')) {
			$multi_promotion = 1;
		}

		$userlevel = Db::name('member_level')->where('aid', aid)->where('id', $this->member['levelid'])->find();
		$adminset = Db::name('admin_set')->where('aid', aid)->find();
		$userinfo = [];
		$userinfo['discount'] = $userlevel['discount'];
		$userinfo['score'] = $this->member['score'];
		$userinfo['score2money'] = $adminset['score2money'];
		$userinfo['scoredk_money'] = round($userinfo['score'] * $userinfo['score2money'], 2);
		$userinfo['scoredkmaxpercent'] = $adminset['scoredkmaxpercent'];
		$userinfo['scoremaxtype'] = 0; //0最大百分比 1最大抵扣金额
		$userinfo['realname'] = $this->member['realname'];
		$userinfo['tel'] = $this->member['tel'];

		$scoredkmaxmoney = 0;
		$allbuydata = [];
		$autofahuo = 0;
		//满减活动
		$mjset = Db::name('manjian_set')->where('aid', aid)->find();
		if ($mjset && $mjset['status'] == 1) {
			$mjdata = json_decode($mjset['mjdata'], true);
			//指定分类
			if ($mjset['fwtype'] == 1) {
				//指定分类或商品分类不存在
				if (empty($mjset['categoryids'])) {
					$mjdata = array();
				}
				//指定商品
			} else if ($mjset['fwtype'] == 2) {
				if (empty($mjset['productids'])) {
					$mjdata = array();
				}
			}
			if (getcustom('plug_tengrui')) {
				if ($mjdata) {
					$tr_check = new \app\common\TengRuiCheck();
					//判断是否是否符合会员认证、会员关系、一户
					$check_manjian = $tr_check->check_manjian($this->member, $mjset);
					if ($check_manjian && $check_manjian['status'] == 0) {
						$mjdata = array();
					}
				}
			}
		} else {
			$mjdata = array();
		}

		foreach ($prodata as $key => $gwc) {
			list($proid, $ggid, $num) = explode(',', $gwc);
			if (getcustom('to86yk')) {
				$field = "id,aid,bid,cid,pic,name,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,freighttype,freightdata,perlimit,gettj,gettjtip,gettjurl,scoredkmaxset,scoredkmaxval,status,start_time,end_time,start_hours,end_hours,balance,no_discount,limit_start,perlimitdan,to86yk_tid";
			} else {
				$field = "id,aid,bid,cid,pic,name,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,freighttype,freightdata,perlimit,gettj,gettjtip,gettjurl,scoredkmaxset,scoredkmaxval,status,start_time,end_time,start_hours,end_hours,balance,no_discount,limit_start,perlimitdan";
			}
			if (getcustom('plug_tengrui')) {
				$field .= ',house_status,group_status,group_ids,is_rzh,relation_type';
			}
			if (getcustom('product_glass')) {
				list($proid, $ggid, $num, $glass_record_id) = explode(',', $gwc);
				$field .= ',product_type';
			}
			$product = Db::name('shop_product')->field($field)->where('aid', aid)->where('ischecked', 1)->where('id', $proid)->find();

			if (!$product) {
				Db::name('shop_cart')->where('aid', aid)->where('proid', $proid)->delete();
				return $this->json(['status' => 0, 'msg' => '产品不存在或已下架']);
			}
			if ($product['status'] == 0) {
				return $this->json(['status' => 0, 'msg' => '商品未上架']);
			}
			if ($product['status'] == 2 && (strtotime($product['start_time']) > time() || strtotime($product['end_time']) < time())) {
				return $this->json(['status' => 0, 'msg' => '商品未上架']);
			}
			if ($product['status'] == 3) {
				$start_time = strtotime(date('Y-m-d ' . $product['start_hours']));
				$end_time = strtotime(date('Y-m-d ' . $product['end_hours']));
				if (($start_time < $end_time && ($start_time > time() || $end_time < time())) || ($start_time >= $end_time && ($start_time > time() && $end_time < time()))) {
					return $this->json(['status' => 0, 'msg' => '商品未上架']);
				}
			}
			if (getcustom('product_glass')) {
				$glassrecord = [];
				if ($glass_record_id) {
					$glassrecord = Db::name('glass_record')->where('aid', aid)->where('mid', $this->mid)->where('id', $glass_record_id)->find();
				}
				$product['glassrecord'] = $glassrecord ?? '';
			}

			if ($product['freighttype'] == 3 || $product['freighttype'] == 4) {
				$autofahuo = $product['freighttype'];
			}

			$guige = Db::name('shop_guige')->where('id', $ggid)->find();
			if (!$guige) {
				Db::name('shop_cart')->where('aid', aid)->where('ggid', $ggid)->delete();
				return $this->json(['status' => 0, 'msg' => '产品该规格不存在或已下架']);
			}
			if ($guige['stock'] < $num) {
				return $this->json(['status' => 0, 'msg' => '库存不足']);
			}
			$gettj = explode(',', $product['gettj']);
			if (!in_array('-1', $gettj) && !in_array($this->member['levelid'], $gettj) && (!in_array('0', $gettj) || $this->member['subscribe'] != 1)) {
				//不是所有人
				if (!$product['gettjtip']) {
					$product['gettjtip'] = '没有权限购买该商品';
				}

				return $this->json(['status' => 0, 'msg' => $product['gettjtip'], 'url' => $product['gettjurl']]);
			}
			if ($product['perlimit'] > 0) {
				$buynum = $num + Db::name('shop_order_goods')->where('aid', aid)->where('mid', mid)->where('proid', $product['id'])->where('status', 'in', '0,1,2,3')->sum('num');
				if ($buynum > $product['perlimit']) {
					return $this->json(['status' => 0, 'msg' => '每人限购' . $product['perlimit'] . '件']);
				}
			}
			if ($guige['limit_start'] > 0 && $num < $guige['limit_start']) {
				return $this->json(['status' => 0, 'msg' => '[' . $product['name'] . '][' . $guige['name'] . '] ' . $guige['limit_start'] . '件起售']);
			}
			if ($product['limit_start'] > 0 && $num < $product['limit_start']) {
				return $this->json(['status' => 0, 'msg' => '[' . $product['name'] . '] ' . $product['limit_start'] . '件起售']);
			}
			if ($product['perlimitdan'] > 0 && $num > $product['perlimitdan']) {
				return $this->json(['status' => 0, 'msg' => '[' . $product['name'] . '] 每单限购' . $product['perlimitdan'] . '件']);
			}
			if (getcustom('plug_tengrui')) {
				//判断是否是否符合会员认证、会员关系、一户
				$tr_check = new \app\common\TengRuiCheck();
				$check_product = $tr_check->check_product($this->member, $product);
				if ($check_product && $check_product['status'] == 0) {
					return $this->json(['status' => $check_product['status'], 'msg' => $check_product['msg']]);
				}
				$tr_roomId = $check_product['tr_roomId'];
			}
			if ($product['lvprice'] == 1) {
				$guige = $this->formatguige($guige, $product['bid']);
			}

			if ($product['scoredkmaxset'] == 0) {
				if ($userinfo['scoredkmaxpercent'] == 0) {
					$userinfo['scoremaxtype'] = 1;
					$scoredkmaxmoney += 0;
				} else {
					if ($userinfo['scoredkmaxpercent'] > 0 && $userinfo['scoredkmaxpercent'] < 100) {
						$scoredkmaxmoney += $userinfo['scoredkmaxpercent'] * 0.01 * $guige['sell_price'] * $num;
					} else {
						$scoredkmaxmoney += $guige['sell_price'] * $num;
					}
				}
			} elseif ($product['scoredkmaxset'] == 1) {
				$userinfo['scoremaxtype'] = 1;
				$scoredkmaxmoney += $product['scoredkmaxval'] * 0.01 * $guige['sell_price'] * $num;
			} elseif ($product['scoredkmaxset'] == 2) {
				$userinfo['scoremaxtype'] = 1;
				$scoredkmaxmoney += $product['scoredkmaxval'] * $num;
			} else {
				$userinfo['scoremaxtype'] = 1;
				$scoredkmaxmoney += 0;
			}

			if (!$allbuydata[$product['bid']]) {
				$allbuydata[$product['bid']] = [];
			}

			if (!$allbuydata[$product['bid']]['prodata']) {
				$allbuydata[$product['bid']]['prodata'] = [];
			}

			$allbuydata[$product['bid']]['prodata'][] = ['product' => $product, 'guige' => $guige, 'num' => $num];
			if ($product['to86yk_tid']) {
				$extendInput = [['key' => 'input', 'val1' => '充值账号', 'val2' => '请输入充值账号', 'val3' => 1], ['key' => 'input', 'val1' => '确认账号', 'val2' => '请再次输入充值账号', 'val3' => 1]];
			}
		}

		if ($autofahuo > 0 && count($prodata) > 1) {
			return $this->json(['status' => 0, 'msg' => '虚拟商品请单独购买']);
		}
		$userinfo['scoredkmaxmoney'] = round($scoredkmaxmoney, 2);

		$havetongcheng = 0;
		foreach ($allbuydata as $bid => $buydata) {

			if (getcustom('guige_split')) {
				$rs = \app\model\ShopProduct::checkstock($buydata['prodata']);
				if ($rs['status'] == 0) {
					return $this->json($rs);
				}

			}
			if ($autofahuo > 0) {
				$freightList = [['id' => 0, 'name' => ($autofahuo == 3 ? '自动发货' : '在线卡密'), 'pstype' => $autofahuo]];
			} else {
				$freightList = \app\model\Freight::getList([['status', '=', 1], ['aid', '=', aid], ['bid', '=', $bid]]);
				$fids = [];
				foreach ($freightList as $v) {
					$fids[] = $v['id'];
				}
				foreach ($buydata['prodata'] as $prodata) {
					if ($prodata['product']['freighttype'] == 0) {
						$fids = array_intersect($fids, explode(',', $prodata['product']['freightdata']));
					} else {
						$thisfreightList = \app\model\Freight::getList([['status', '=', 1], ['aid', '=', aid], ['bid', '=', $bid]]);
						$thisfids = [];
						foreach ($thisfreightList as $v) {
							$thisfids[] = $v['id'];
						}
						$fids = array_intersect($fids, $thisfids);
					}
				}
				if (!$fids) {
					if (count($buydata['prodata']) > 1) {
						return $this->json(['status' => 0, 'msg' => '所选择商品配送方式不同，请分别下单']);
					} else {
						return $this->json(['status' => 0, 'msg' => '获取配送方式失败']);
					}
				}
				$freightList = \app\model\Freight::getList([['status', '=', 1], ['aid', '=', aid], ['bid', '=', $bid], ['id', 'in', $fids]]);
				foreach ($freightList as $k => $v) {
					//var_dump($freightList);
					if ($v['pstype'] == 2) {
						//同城配送
						$havetongcheng = 1;
					}
					if (getcustom('hmy_yuyue') && $v['pstype'] == 12) {
						//红蚂蚁定制
						$havetongcheng = 1;
					}
				}
			}
			$allbuydata[$bid]['freightList'] = $freightList;
		}
		if ($havetongcheng) {
			$address = Db::name('member_address')->where('aid', aid)->where('mid', mid)->where('latitude', '>', 0)->order('isdefault desc,id desc')->find();
		} else {
			$address = Db::name('member_address')->where('aid', aid)->where('mid', mid)->order('isdefault desc,id desc')->find();
		}
		if (!$address) {
			$address = [];
		}

		$needLocation = 0;
		$allproduct_price = 0;
		foreach ($allbuydata as $bid => $buydata) {
			if ($bid != 0) {
				$business = Db::name('business')->where('id', $bid)->field('id,aid,cid,name,logo,tel,address,sales,longitude,latitude,start_hours,end_hours,start_hours2,end_hours2,start_hours3,end_hours3,invoice,invoice_type')->find();

				$is_open = 0;
				if ($is_open == 0) {
					if ($business['start_hours'] != $business['end_hours']) {
						$start_time = strtotime(date('Y-m-d ' . $business['start_hours']));
						$end_time = strtotime(date('Y-m-d ' . $business['end_hours']));
						if (($start_time < $end_time && ($start_time > time() || $end_time < time())) || ($start_time > $end_time && ($start_time > time() && $end_time < time()))) {
							//return $this->json(['status'=>-4,'msg'=>'商家不在营业时间']);
						} else {
							$is_open = 1;
						}
					} else {
						$is_open = 1;
					}
				}
				if ($is_open == 0) {
					$start_time = strtotime(date('Y-m-d ' . $business['start_hours2']));
					$end_time = strtotime(date('Y-m-d ' . $business['end_hours2']));
					if ($start_time == $end_time || ($start_time < $end_time && ($start_time > time() || $end_time < time())) || ($start_time > $end_time && ($start_time > time() && $end_time < time()))) {
						//return $this->json(['status'=>-4,'msg'=>'商家不在营业时间']);
					} else {
						$is_open = 1;
					}
				}
				if ($is_open == 0) {
					$start_time = strtotime(date('Y-m-d ' . $business['start_hours3']));
					$end_time = strtotime(date('Y-m-d ' . $business['end_hours3']));
					if ($start_time == $end_time || ($start_time < $end_time && ($start_time > time() || $end_time < time())) || ($start_time > $end_time && ($start_time > time() && $end_time < time()))) {
						//return $this->json(['status'=>-4,'msg'=>'商家不在营业时间']);
					} else {
						$is_open = 1;
					}
				}
				if ($is_open == 0) {
					return $this->json(['status' => -4, 'msg' => '商家不在营业时间']);
				}
			} else {
				$business = Db::name('admin_set')->where('aid', aid)->field('id,name,logo,desc,tel,invoice,invoice_type,invoice_rate')->find();
			}
			$business['invoice_type'] = $business['invoice'] ? explode(',', $business['invoice_type']) : [];
			$product_priceArr = [];
			$product_price = 0;
			$needzkproduct_price = 0;
			$totalweight = 0;
			$totalnum = 0;
			$prodataArr = [];
			$proids = [];
			$cids = [];
			//符合满减的商品价格
			$mj_price = 0;
			foreach ($buydata['prodata'] as $prodata) {
				$product_priceArr[] = $prodata['guige']['sell_price'] * $prodata['num'];
				$product_price += $prodata['guige']['sell_price'] * $prodata['num'];
				if ($prodata['product']['balance']) {
					$product_price = $product_price * (1 - $prodata['product']['balance'] * 0.01);
				}
				if ($prodata['product']['lvprice'] == 0 && $prodata['product']['no_discount'] == 0) {
					//未开启会员价
					$needzkproduct_price += $prodata['guige']['sell_price'] * $prodata['num'];
				}
				$totalweight += $prodata['guige']['weight'] * $prodata['num'];
				$totalnum += $prodata['num'];
				$_prostr = $prodata['product']['id'] . ',' . $prodata['guige']['id'] . ',' . $prodata['num'];
				if (isset($prodata['product']['glassrecord']) && $prodata['product']['glassrecord']) {
					$_prostr .= ',' . $prodata['product']['glassrecord']['id'];
				}
				$prodataArr[] = $_prostr;
				$proids[] = $prodata['product']['id'];
				$cids = array_merge(explode(',', $prodata['product']['cid']), $cids);
				//如果满减设置存在，且数据存在
				if ($mjset && $mjdata) {
					//指定分类
					if ($mjset['fwtype'] == 1) {
						//指定分类数组
						$cat_arr = explode(",", $mjset['categoryids']);
						//商品分类
						$pro_cat_arr = explode(",", $prodata['product']['cid']);
						//交集
						$j_arr = array_intersect($cat_arr, $pro_cat_arr);
						if ($j_arr) {
							$mj_price += $prodata['guige']['sell_price'] * $prodata['num'];
							if ($prodata['product']['balance']) {
								$mj_price = $mj_price * (1 - $prodata['product']['balance'] * 0.01);
							}
						}
						//指定商品
					} else if ($mjset['fwtype'] == 2) {
						$pro_arr = explode(",", $mjset['productids']);
						//商品在指定商品内
						if (in_array($prodata['product']['id'], $pro_arr)) {
							$mj_price += $prodata['guige']['sell_price'] * $prodata['num'];
							if ($prodata['product']['balance']) {
								$mj_price = $mj_price * (1 - $prodata['product']['balance'] * 0.01);
							}
						}
					} else {
						$mj_price += $prodata['guige']['sell_price'] * $prodata['num'];
						if ($prodata['product']['balance']) {
							$mj_price = $mj_price * (1 - $prodata['product']['balance'] * 0.01);
						}
					}
				}
			}
			$prodatastr = implode('-', $prodataArr);

			$rs = \app\model\Freight::formatFreightList($buydata['freightList'], $address, $product_price, $totalnum, $totalweight);

			$freightList = $rs['freightList'];
			$freightArr = $rs['freightArr'];
			if ($rs['needLocation'] == 1) {
				$needLocation = 1;
			}

			$leveldk_money = 0;
			if ($userlevel && $userlevel['discount'] > 0 && $userlevel['discount'] < 10) {
				$leveldk_money = $needzkproduct_price * (1 - $userlevel['discount'] * 0.1);
			}
			$leveldk_money = round($leveldk_money, 2);
			$price = $product_price - $leveldk_money;
			$mj_price = $mj_price - $leveldk_money;

			$manjian_money = 0;
			$moneyduan = 0;
			if ($mjdata && $mj_price > 0) {
				//如果是总满减
				if ($mjset['total_status'] == 1) {
					//指定分类
					if ($mjset['fwtype'] == 1) {
						//查询他分类消费累计
						$sum_money = Db::name('shop_order_goods')
							->alias('sog')
							->join('shop_order so', 'so.id   = sog.orderid')
							->join('shop_product sp', 'sp.id = sog.proid')
							->where('sog.mid', mid)
							->where('so.status', 3)
							->where('sp.cid', 'in', $mjset['categoryids'])
							->sum('sog.totalprice');

						//分类退款累计
						$refund_money = Db::name('shop_refund_order_goods')
							->alias('srog')
							->join('shop_order so', 'so.id   = srog.orderid')
							->join('shop_product sp', 'sp.id = srog.proid')
							->where('srog.mid', mid)
							->where('so.status', 3)
							->where('so.refund_status', 2)
							->where('sp.cid', 'in', $mjset['categoryids'])
							->sum('srog.refund_money');

						//指定商品
					} else if ($mjset['fwtype'] == 2) {
						//查询他商品消费累计
						$sum_money = Db::name('shop_order_goods')
							->alias('sog')
							->join('shop_order so', 'so.id   = sog.orderid')
							->join('shop_product sp', 'sp.id = sog.proid')
							->where('sog.mid', mid)
							->where('so.status', 3)
							->where('sog.proid', 'in', $mjset['productids'])
							->sum('sog.totalprice');

						//商品退款累计
						$refund_money = Db::name('shop_refund_order_goods')
							->alias('srog')
							->join('shop_order so', 'so.id   = srog.orderid')
							->join('shop_product sp', 'sp.id = srog.proid')
							->where('srog.mid', mid)
							->where('so.status', 3)
							->where('so.refund_status', 2)
							->where('srog.proid', 'in', $mjset['productids'])
							->sum('srog.refund_money');
						//所有
					} else {
						//查询他消费累计
						$sum_money = Db::name('shop_order')->where('mid', mid)->where('status', 3)->sum('totalprice');
						//退款累计
						$refund_money = Db::name('shop_order')->where('mid', mid)->where('status', 3)->where('refund_status', 2)->sum('refund_money');
					}

					//实际累计
					$sj_money = $sum_money - $refund_money;
					$sj_money = round($sj_money, 2);
					$all_price = $sj_money + $mj_price;
					foreach ($mjdata as $give) {
						if (($all_price - $leveldk_money) * 1 >= $give['money'] * 1 && $give['money'] * 1 > $moneyduan) {
							$moneyduan = $give['money'] * 1;
							$manjian_money = $give['jian'] * 1;
						}
					}
				} else {
					foreach ($mjdata as $give) {
						if (($mj_price - $leveldk_money) * 1 >= $give['money'] * 1 && $give['money'] * 1 > $moneyduan) {
							$moneyduan = $give['money'] * 1;
							$manjian_money = $give['jian'] * 1;
						}
					}
				}

			}
			if ($manjian_money > 0) {
				$allbuydata[$bid]['manjian_money'] = round($manjian_money, 2);
			} else {
				$allbuydata[$bid]['manjian_money'] = 0;
			}

			$newcouponlist = [];
			$couponList = Db::name('coupon_record')
				->where("bid=-1 or bid=" . $bid)->where('aid', aid)->where('mid', mid)->where('type', 'in', '1,4,10')->where('status', 0)->where('minprice', '<=', $price - $manjian_money)->where('starttime', '<=', time())->where('endtime', '>', time())
				->order('id desc')->select()->toArray();
			if (!$couponList) {
				$couponList = [];
			}

			foreach ($couponList as $k => $v) {
				//$couponList[$k]['starttime'] = date('m-d H:i',$v['starttime']);
				//$couponList[$k]['endtime'] = date('m-d H:i',$v['endtime']);
				$couponinfo = Db::name('coupon')->where('aid', aid)->where('id', $v['couponid'])->find();
				//0全场通用,1指定类目,2指定商品
				if (!in_array($couponinfo['fwtype'], [0, 1, 2])) {
					continue;
				}

				$usetj = explode(',', $couponinfo['usetj']);
				if (!in_array('-1', $usetj) && !in_array($this->member['levelid'], $usetj) && (!in_array('0', $usetj) || $this->member['subscribe'] != 1)) {
					continue;
				}

				$v['thistotalprice'] = $price - $manjian_money;
				if ($couponinfo['fwtype'] == 2) {
//指定商品可用
					$productids = explode(',', $couponinfo['productids']);
					if (!array_intersect($proids, $productids)) {
						continue;
					}
					$thistotalprice = 0;
					foreach ($buydata['prodata'] as $k2 => $v2) {
						$product = $v2['product'];
						if (in_array($product['id'], $productids)) {
							$thistotalprice += $v2['guige']['sell_price'] * $v2['num'];
						}
					}
					if ($thistotalprice < $v['minprice']) {
						continue;
					}
					$v['thistotalprice'] = $thistotalprice;
				}
				if ($couponinfo['fwtype'] == 1) {
//指定类目可用
					$categoryids = explode(',', $couponinfo['categoryids']);
					$clist = Db::name('shop_category')->where('pid', 'in', $categoryids)->select()->toArray();
					foreach ($clist as $kc => $vc) {
						$categoryids[] = $vc['id'];
						$cate2 = Db::name('shop_category')->where('pid', $vc['id'])->find();
						$categoryids[] = $cate2['id'];
					}
					if (!array_intersect($cids, $categoryids)) {
						continue;
					}
					$thistotalprice = 0;
					foreach ($buydata['prodata'] as $k2 => $v2) {
						$product = $v2['product'];
						if (array_intersect(explode(',', $product['cid']), $categoryids)) {
							$thistotalprice += $v2['guige']['sell_price'] * $v2['num'];
						}
					}
					if ($thistotalprice < $v['minprice']) {
						continue;
					}
					$v['thistotalprice'] = $thistotalprice;
				}
				if ($v['bid'] > 0) {
					$binfo = Db::name('business')->where('aid', aid)->where('id', $v['bid'])->find();
					$v['bname'] = $binfo['name'];
				}
				$newcouponlist[] = $v;
			}
			$couponList = $newcouponlist;

			//促销活动
			$cuxiaolist = Db::name('cuxiao')->where('aid', aid)->where('bid', $bid)->where("(type in (1,2,3,4) and minprice<=" . ($price - $manjian_money) . ") or ((type=5 or type=6) and minnum<=" . $totalnum . ") ")->where('starttime', '<', time())->where('endtime', '>', time())->order('sort desc')->select()->toArray();
			$newcxlist = [];
			foreach ($cuxiaolist as $k => $v) {
				$gettj = explode(',', $v['gettj']);
				if (!in_array('-1', $gettj) && !in_array($this->member['levelid'], $gettj)) {
					//不是所有人
					continue;
				}
				if ($v['fwtype'] == 2) {
//指定商品可用
					$productids = explode(',', $v['productids']);
					if (!array_intersect($proids, $productids)) {
						continue;
					}
					if ($v['type'] == 1 || $v['type'] == 2 || $v['type'] == 3 || $v['type'] == 4) {
//指定商品是否达到金额要求
						$thistotalprice = 0;
						foreach ($buydata['prodata'] as $k2 => $v2) {
							$product = $v2['product'];
							if (in_array($product['id'], $productids)) {
								$thistotalprice += $v2['guige']['sell_price'] * $v2['num'];
							}
						}
						if ($thistotalprice < $v['minprice']) {
							continue;
						}
					}
					if ($v['type'] == 6 || $v['type'] == 5) {
//指定商品是否达到件数要求
						$thistotalnum = 0;
						foreach ($buydata['prodata'] as $k2 => $v2) {
							$product = $v2['product'];
							if (in_array($product['id'], $productids)) {
								$thistotalnum += $v2['num'];
							}
						}
						if ($thistotalnum < $v['minnum']) {
							continue;
						}
					}
				}
				if ($v['fwtype'] == 1) {
//指定类目可用
					$categoryids = explode(',', $v['categoryids']);
					$clist = Db::name('shop_category')->where('pid', 'in', $categoryids)->select()->toArray();
					foreach ($clist as $kc => $vc) {
						$categoryids[] = $vc['id'];
						$cate2 = Db::name('shop_category')->where('pid', $vc['id'])->find();
						$categoryids[] = $cate2['id'];
					}
					if (!array_intersect($cids, $categoryids)) {
						continue;
					}
					if ($v['type'] == 1 || $v['type'] == 2 || $v['type'] == 3 || $v['type'] == 4) {
//指定商品是否达到金额要求
						$thistotalprice = 0;
						foreach ($buydata['prodata'] as $k2 => $v2) {
							$product = $v2['product'];
							if (array_intersect(explode(',', $product['cid']), $categoryids)) {
								$thistotalprice += $v2['guige']['sell_price'] * $v2['num'];
							}
						}
						if ($thistotalprice < $v['minprice']) {
							continue;
						}
					}
					if ($v['type'] == 6 || $v['type'] == 5) {
//指定类目内商品是否达到件数要求
						$thistotalnum = 0;
						foreach ($buydata['prodata'] as $k2 => $v2) {
							$product = $v2['product'];
							if (array_intersect(explode(',', $product['cid']), $categoryids)) {
								$thistotalnum += $v2['num'];
							}
						}
						if ($thistotalnum < $v['minnum']) {
							continue;
						}
					}
				}
				if ($v['type'] == 4 || $v['type'] == 5) {
					if ($v['fwtype'] == 2) {
						//商品
						$cuxiaomoney = 0;
						$prozkArr = array_combine(explode(',', $v['productids']), explode(',', $v['prozk']));
						$pronumArr = array_combine(explode(',', $v['productids']), explode(',', $v['pronum']));
						foreach ($buydata['prodata'] as $k2 => $v2) {
							$product = $v2['product'];
							if ($prozkArr[$product['id']]) {
								$prozk = $prozkArr[$product['id']];
							} elseif (isset($prozkArr[$product['id']])) {
								$prozk = $v['zhekou'];
							} else {
								$prozk = 10;
							}
							if ($v['type'] == 5 && $pronumArr[$product['id']] && intval($pronumArr[$product['id']]) > $v2['num']) {
								$prozk = 10;
							}
							$cuxiaomoney += $product_priceArr[$k2] * (1 - $prozk * 0.1);
						}
					} elseif ($v['fwtype'] == 1) {
						//分类
						$categoryPrice = 0;

						foreach ($buydata['prodata'] as $k2 => $v2) {
							$product = $v2['product'];
							$cids2 = explode(',', $product['cid']);
							if (array_intersect($cids2, $categoryids)) {
								$categoryPrice += $v2['guige']['sell_price'] * $v2['num'];
							}
						}
						$cuxiaomoney = $categoryPrice * (1 - $v['zhekou'] * 0.1);
					} else {
						//全部
						$cuxiaomoney = $price * (1 - $v['zhekou'] * 0.1);
					}
					$v['cuxiaomoney'] = round($cuxiaomoney, 2);
				}
				if (getcustom('plug_tengrui')) {
					$tr_check = new \app\common\TengRuiCheck();
					//判断是否是否符合会员认证、会员关系、一户
					$check_cuxiao = $tr_check->check_cuxiao($this->member, $v);
					if ($check_cuxiao && $check_cuxiao['status'] == 0) {
						continue;
					}
				}
				$newcxlist[] = $v;
			}

			if ($extendInput) {
				foreach ($freightList as $fk => $fv) {
					$freightList[$fk]['formdata'] = array_merge($extendInput, $fv['formdata']);
				}
			}
			$invoice_price = 0;
			if ($business['invoice'] && $business['invoice_rate'] > 0 && getcustom('invoice_rate')) {
				$invoice_price = ($product_price + $freightList[0]['freight_price']) * $business['invoice_rate'] / 100;
			}

			$allbuydata[$bid]['bid'] = $bid;
			$allbuydata[$bid]['business'] = $business;
			$allbuydata[$bid]['prodatastr'] = $prodatastr;
			$allbuydata[$bid]['couponList'] = $couponList;
			$allbuydata[$bid]['couponCount'] = count($couponList);
			$allbuydata[$bid]['freightList'] = $freightList;
			$allbuydata[$bid]['freightArr'] = $freightArr;
			$allbuydata[$bid]['product_price'] = round($product_price, 2);
			$allbuydata[$bid]['invoice_price'] = round($invoice_price, 2);
			$allbuydata[$bid]['leveldk_money'] = $leveldk_money;
			$allbuydata[$bid]['coupon_money'] = 0;
			$allbuydata[$bid]['coupontype'] = 1;
			$allbuydata[$bid]['couponrid'] = 0;
			$allbuydata[$bid]['couponrids'] = [];
			$allbuydata[$bid]['coupons'] = [];
			$allbuydata[$bid]['freightkey'] = 0;
			$allbuydata[$bid]['pstimetext'] = '';
			$allbuydata[$bid]['freight_time'] = '';
			$allbuydata[$bid]['storeid'] = 0;
			$allbuydata[$bid]['storename'] = '';
			$allbuydata[$bid]['cuxiaolist'] = $newcxlist;
			$allbuydata[$bid]['cuxiaoCount'] = count($newcxlist);
			$allbuydata[$bid]['cuxiao_money'] = 0;
			$allbuydata[$bid]['cuxiaotype'] = 0;
			$allbuydata[$bid]['cuxiaoid'] = 0;
			$allbuydata[$bid]['editorFormdata'] = [];

			$allbuydata[$bid]['coupon_peruselimit'] = 1;
			if (getcustom('coupon_peruselimit')) {
				$shopset = Db::name('shop_sysset')->where('aid', aid)->find();
				$allbuydata[$bid]['coupon_peruselimit'] = $shopset['coupon_peruselimit'];
			}

			$allproduct_price += $product_price;
		}

		if (getcustom('plug_xiongmao')) {
			$admin = Db::name('admin')->where('id', aid)->find();
			if (in_array('order_change_price', explode(',', $admin['remark']))) {
				$order_change_price = true;
			}
		}

		$rdata = [];
		$rdata['status'] = 1;
		$rdata['havetongcheng'] = $havetongcheng;
		$rdata['address'] = $address;
		$rdata['linkman'] = $address ? $address['name'] : strval($userinfo['realname']);
		$rdata['tel'] = $address ? $address['tel'] : strval($userinfo['tel']);
		if (!$rdata['linkman']) {
			$lastorder = Db::name('shop_order')->where('aid', aid)->where('mid', mid)->where('linkman', '<>', '')->find();
			if ($lastorder) {
				$rdata['linkman'] = $lastorder['linkman'];
				$rdata['tel'] = $lastorder['tel'];
			}
		}
		$rdata['userinfo'] = $userinfo;
		$rdata['allbuydata'] = $allbuydata;
		$rdata['needLocation'] = $needLocation;
		$rdata['scorebdkyf'] = Db::name('admin_set')->where('aid', aid)->value('scorebdkyf');
		$rdata['buy_selectmember'] = false;
		if (getcustom('buy_selectmember')) {
			$canselect = Db::name('member_level')->where('aid', aid)->where('can_buyselect', 1)->find();
			if ($canselect) {
				$rdata['buy_selectmember'] = true;
			}

		}
		$rdata['multi_promotion'] = $multi_promotion;
		$rdata['order_change_price'] = $order_change_price;
		$rdata['pstype3needAddress'] = false;
		if (getcustom('plug_zhiming')) {
			$rdata['pstype3needAddress'] = true;
		}
		return $this->json($rdata);
	}
	public function createOrder() {
		$this->checklogin();
		$sysset = Db::name('admin_set')->where('aid', aid)->find();
		$post = input('post.');
		//收货地址
		if ($post['addressid'] == '' || $post['addressid'] == 0) {
			$address = ['id' => 0, 'name' => $post['linkman'], 'tel' => $post['tel'], 'area' => '', 'address' => ''];
		} else {
			$address = Db::name('member_address')->where('id', $post['addressid'])->where('aid', aid)->where('mid', mid)->find();
			if (!$address) {
				return $this->json(['status' => 0, 'msg' => '所选收货地址不存在']);
			}

		}
		$userlevel = Db::name('member_level')->where('aid', aid)->where('id', $this->member['levelid'])->find();
		$shopset = Db::name('shop_sysset')->where('aid', aid)->find();

		$buydata = $post['buydata'];
		$couponridArr = [];
		foreach ($buydata as $data) {
			//判断有没有重复选择的优惠券
			if (!$data['couponrid']) {
				continue;
			}

			$couponrids = explode(',', $data['couponrid']);
			if ($shopset['coupon_peruselimit'] < count($couponrids)) {
				return $this->json(['status' => 0, 'msg' => '最多可用' . $shopset['coupon_peruselimit'] . '张' . t('优惠券')]);
			}
			foreach ($couponrids as $couponrid) {
				if (in_array($couponrid, $couponridArr)) {
					return $this->json(['status' => 0, 'msg' => t('优惠券') . '不可重复使用']);
				}
				$couponridArr[] = $couponrid;
			}
		}

		$ordernum = \app\common\Common::generateOrderNo(aid);
		$scoredkmaxmoney = 0;
		$scoremaxtype = 0;
		$i = 0;
		$alltotalprice = 0;
		$alltotalscore = 0;

		//满减活动
		$mjset = Db::name('manjian_set')->where('aid', aid)->find();
		if ($mjset && $mjset['status'] == 1) {
			$mjdata = json_decode($mjset['mjdata'], true);
			//指定分类
			if ($mjset['fwtype'] == 1) {
				//指定分类或商品分类不存在
				if (empty($mjset['categoryids'])) {
					$mjdata = array();
				}
				//指定商品
			} else if ($mjset['fwtype'] == 2) {
				if (empty($mjset['productids'])) {
					$mjdata = array();
				}
			}
			if (getcustom('plug_tengrui')) {
				if ($mjdata) {
					$tr_check = new \app\common\TengRuiCheck();
					//判断是否是否符合会员认证、会员关系、一户、用户组，不符合则直接去掉
					$check_manjian = $tr_check->check_manjian($this->member, $mjset);
					if ($check_manjian && $check_manjian['status'] == 0) {
						$mjdata = array();
					}
					$manjian_tr_roomId = $check_manjian['tr_roomId'];
				}
			}
		} else {
			$mjdata = array();
		}

		foreach ($buydata as $data) {
			$i++;
			$bid = $data['bid'];
			if ($data['prodata']) {
				$prodata = explode('-', $data['prodata']);
			} else {
				return $this->json(['status' => 0, 'msg' => '产品数据错误']);
			}
			$product_priceArr = [];
			$product_price = 0;
			$balance_price = 0;
			$needzkproduct_price = 0;
			$givescore = 0; //奖励积分 确认收货后赠送
			$givescore2 = 0; //奖励积分2 付款后赠送
			$totalweight = 0; //重量
			$totalnum = 0;
			$prolist = [];
			$proids = [];
			$cids = [];
			$invoice = [];

			if (getcustom('pay_yuanbao')) {
				$total_yuanbao = 0; //总元宝价格
				$have_no_yuanbao = 0; //是否有非元宝商品
			}

			if ($bid) {
				$store_info = Db::name('business')->where('aid', aid)->where('id', $bid)->find();
			} else {
				$store_info = Db::name('admin_set')->where('aid', aid)->find();
			}

			$store_name = $store_info['name'];

			$freightList = \app\model\Freight::getList([['status', '=', 1], ['aid', '=', aid], ['bid', '=', $bid]]);

			$fids = [];
			foreach ($freightList as $v) {
				$fids[] = $v['id'];
			}
			//			dd($data['prodataList']);
			$extendInput = [];
			//符合满减的商品价格
			$mj_price = 0;
			foreach ($prodata as $key => $pro) {
				$sdata = explode(',', $pro);
				$sdata[2] = intval($sdata[2]);
				if ($sdata[2] <= 0) {
					return $this->json(['status' => 0, 'msg' => '购买数量有误']);
				}

				$product = Db::name('shop_product')->where('aid', aid)->where('ischecked', 1)->where('bid', $bid)->where('id', $sdata[0])->find();
				if (!$product) {
					return $this->json(['status' => 0, 'msg' => '产品不存在或已下架']);
				}

				if ($product['status'] == 0) {
					return $this->json(['status' => 0, 'msg' => '商品未上架']);
				}
				if ($product['status'] == 2 && (strtotime($product['start_time']) > time() || strtotime($product['end_time']) < time())) {
					return $this->json(['status' => 0, 'msg' => '商品未上架']);
				}
				if ($product['status'] == 3) {
					$start_time = strtotime(date('Y-m-d ' . $product['start_hours']));
					$end_time = strtotime(date('Y-m-d ' . $product['end_hours']));
					if (($start_time < $end_time && ($start_time > time() || $end_time < time())) || ($start_time >= $end_time && ($start_time > time() && $end_time < time()))) {
						return $this->json(['status' => 0, 'msg' => '商品未上架']);
					}
				}

				if (getcustom('shopcate_time')) {
					$procids = explode(',', $product['cid']);
					$cate = Db::name('shop_category')->where('aid', aid)->where('id', 'in', $product['cid'])->select()->toArray();
					foreach ($cate as $c) {
						if ($c['start_hours'] && $c['end_hours']) {
							$catestart_time = strtotime(date('Y-m-d ' . $c['start_hours']));
							$cateend_time = strtotime(date('Y-m-d ' . $c['end_hours']));
							if (($catestart_time < $cateend_time && ($catestart_time > time() || $cateend_time < time())) || ($catestart_time >= $cateend_time && ($catestart_time > time() && $cateend_time < time()))) {
								return $this->json(['status' => 0, 'msg' => '商品购买时间' . $c['start_hours'] . '-' . $c['end_hours'] . '请稍后再来']);
							}
						}

					}
				}

				if ($key == 0) {
					$title = $product['name'];
				}

				$guige = Db::name('shop_guige')->where('aid', aid)->where('id', $sdata[1])->find();
				if (!$guige) {
					return $this->json(['status' => 0, 'msg' => '产品规格不存在或已下架']);
				}

				if ($guige['stock'] < $sdata[2]) {
					return $this->json(['status' => 0, 'msg' => '库存不足']);
				}
				$gettj = explode(',', $product['gettj']);
				if (!in_array('-1', $gettj) && !in_array($this->member['levelid'], $gettj) && (!in_array('0', $gettj) || $this->member['subscribe'] != 1)) {
					//不是所有人
					if (!$product['gettjtip']) {
						$product['gettjtip'] = '没有权限购买该商品';
					}

					return $this->json(['status' => 0, 'msg' => $product['gettjtip'], 'url' => $product['gettjurl']]);
				}
				if ($product['perlimit'] > 0) {
					$buynum = $sdata[2] + Db::name('shop_order_goods')->where('aid', aid)->where('mid', mid)->where('proid', $product['id'])->where('status', 'in', '0,1,2,3')->sum('num');
					if ($buynum > $product['perlimit']) {
						return $this->json(['status' => 0, 'msg' => '每人限购' . $product['perlimit'] . '件']);
					}
				}
				if ($guige['limit_start'] > 0 && $sdata[2] < $guige['limit_start']) {
					return $this->json(['status' => 0, 'msg' => '[' . $product['name'] . '][' . $guige['name'] . '] ' . $guige['limit_start'] . '件起售']);
				}
				if ($product['limit_start'] > 0 && $sdata[2] < $product['limit_start']) {
					return $this->json(['status' => 0, 'msg' => $product['limit_start'] . '件起售']);
				}
				if ($product['perlimitdan'] > 0 && $sdata[2] > $product['perlimitdan']) {
					return $this->json(['status' => 0, 'msg' => '[' . $product['name'] . '] 每单限购' . $product['perlimitdan'] . '件']);
				}
				if (getcustom('plug_tengrui')) {
					//判断是否是否符合会员认证、会员关系、一户、用户组
					$tr_check = new \app\common\TengRuiCheck();
					$check_product = $tr_check->check_product($this->member, $product);
					if ($check_product && $check_product['status'] == 0) {
						return $this->json(['status' => $check_product['status'], 'msg' => $check_product['msg']]);
					}
					$tr_roomId = $check_product['tr_roomId'];
					$product['tr_roomId'] = $tr_roomId;
				}
				if ($product['lvprice'] == 1) {
					$guige = $this->formatguige($guige, $product['bid']);
				}

				if (getcustom('plug_xiongmao') && $data['prodataList']) {
					//自定义价格
					if ($data['prodataList'][$key]['guige']['sell_price'] < $guige['sell_price']) {
						return $this->json(['status' => 0, 'msg' => '“' . $product['name'] . '”不能小于原价']);
					}
					$guige['sell_price'] = $data['prodataList'][$key]['guige']['sell_price'];
				}
				$product_price += $guige['sell_price'] * $sdata[2];
				if ($product['balance']) {
					$balance_price += $product_price * $product['balance'] * 0.01;
					$product_price = $product_price * (1 - $product['balance'] * 0.01);
				}
				$product_priceArr[] = $guige['sell_price'] * $sdata[2];
				if ($product['lvprice'] == 0 && $product['no_discount'] == 0) {
					//未开启会员价
					$needzkproduct_price += $guige['sell_price'] * $sdata[2];
				}
				$totalweight += $guige['weight'] * $sdata[2];
				$totalnum += $sdata[2];

				if ($product['scoredkmaxset'] == 0) {
					if ($sysset['scoredkmaxpercent'] == 0) {
						$scoredkmaxmoney += 0;
					} else {
						if ($sysset['scoredkmaxpercent'] > 0 && $sysset['scoredkmaxpercent'] < 100) {
							$scoredkmaxmoney += $sysset['scoredkmaxpercent'] * 0.01 * $guige['sell_price'] * $sdata[2];
						} else {
							$scoredkmaxmoney += $guige['sell_price'] * $sdata[2];
						}
					}
				} elseif ($product['scoredkmaxset'] == 1) {
					$scoremaxtype = 1;
					$scoredkmaxmoney += $product['scoredkmaxval'] * 0.01 * $guige['sell_price'] * $sdata[2];
				} elseif ($product['scoredkmaxset'] == 2) {
					$scoremaxtype = 1;
					$scoredkmaxmoney += $product['scoredkmaxval'] * $sdata[2];
				} else {
					$scoremaxtype = 1;
					$scoredkmaxmoney += 0;
				}
				//视力档案
				$glass_record_id = 0;
				if (getcustom('product_glass')) {
					$glass_record_id = $sdata[3] ?? 0;
				}

				$prolist[] = ['product' => $product, 'guige' => $guige, 'num' => $sdata[2], 'isSeckill' => 0, 'glass_record_id' => $glass_record_id];

				if ($product['freighttype'] == 0) {
					$fids = array_intersect($fids, explode(',', $product['freightdata']));
				} else {
					$thisfreightList = \app\model\Freight::getList([['status', '=', 1], ['aid', '=', aid], ['bid', '=', $bid]]);
					$thisfids = [];
					foreach ($thisfreightList as $v) {
						$thisfids[] = $v['id'];
					}
					$fids = array_intersect($fids, $thisfids);
				}
				$proids[] = $product['id'];
				$cids = array_merge($cids, explode(',', $product['cid']));
				if ($product['givescore_time'] == 0) {
					$givescore += $guige['givescore'] * $sdata[2];
				} else {
					$givescore2 += $guige['givescore'] * $sdata[2];
				}

				if ($product['to86yk_tid']) {
					$extendInput = [['key' => 'input', 'val1' => '充值账号', 'val2' => '请输入充值账号', 'val3' => 1], ['key' => 'input', 'val1' => '确认账号', 'val2' => '请再次输入充值账号', 'val3' => 1]];
				}
				if (getcustom('pay_yuanbao')) {
					if ($product['yuanbao'] > 0) {
						$total_yuanbao += $product['yuanbao'];
					} else {
						$have_no_yuanbao = 1;
					}
				}
				//如果存在，且数据存在
				if ($mjset && $mjdata) {
					//指定分类
					if ($mjset['fwtype'] == 1) {
						//指定分类数组
						$cat_arr = explode(",", $mjset['categoryids']);
						//商品分类
						$pro_cat_arr = explode(",", $product['cid']);
						//交集
						$j_arr = array_intersect($cat_arr, $pro_cat_arr);
						if ($j_arr) {
							$mj_price += $guige['sell_price'] * $sdata[2];
							if ($product['balance']) {
								$mj_price = $mj_price * (1 - $product['balance'] * 0.01);
							}
						}
						//指定商品
					} else if ($mjset['fwtype'] == 2) {
						$pro_arr = explode(",", $mjset['productids']);
						//商品在指定商品内
						if (in_array($product['id'], $pro_arr)) {
							$mj_price += $guige['sell_price'] * $sdata[2];
							if ($product['balance']) {
								$mj_price = $mj_price * (1 - $product['balance'] * 0.01);
							}
						}
					} else {
						$mj_price += $guige['sell_price'] * $sdata[2];
						if ($product['balance']) {
							$mj_price = $mj_price * (1 - $product['balance'] * 0.01);
						}
					}
				}
			}
			if (!$fids) {
				if (count($buydata['prodata']) > 1) {
					return $this->json(['status' => 0, 'msg' => '所选择商品配送方式不同，请分别下单']);
				} else {
					return $this->json(['status' => 0, 'msg' => '获取配送方式失败']);
				}
			}
			if (getcustom('guige_split')) {
				$rs = \app\model\ShopProduct::checkstock($prolist);
				if ($rs['status'] == 0) {
					return $this->json($rs);
				}

			}
			//会员折扣
			$leveldk_money = 0;
			if ($userlevel && $userlevel['discount'] > 0 && $userlevel['discount'] < 10) {
				$leveldk_money = $needzkproduct_price * (1 - $userlevel['discount'] * 0.1);
			}
			$totalprice = $product_price - $leveldk_money;
			$mj_price = $mj_price - $leveldk_money;

			$manjian_money = 0;
			$moneyduan = 0;
			if ($mjdata && $mj_price > 0) {
				//如果是总满减
				if ($mjset['total_status'] == 1) {
					//指定分类
					if ($mjset['fwtype'] == 1) {
						//查询他分类消费累计
						$sum_money = Db::name('shop_order_goods')
							->alias('sog')
							->join('shop_order so', 'so.id   = sog.orderid')
							->join('shop_product sp', 'sp.id = sog.proid')
							->where('sog.mid', mid)
							->where('so.status', 3)
							->where('sp.cid', 'in', $mjset['categoryids'])
							->sum('sog.totalprice');

						//分类退款累计
						$refund_money = Db::name('shop_refund_order_goods')
							->alias('srog')
							->join('shop_order so', 'so.id   = srog.orderid')
							->join('shop_product sp', 'sp.id = srog.proid')
							->where('srog.mid', mid)
							->where('so.status', 3)
							->where('so.refund_status', 2)
							->where('sp.cid', 'in', $mjset['categoryids'])
							->sum('srog.refund_money');

						//指定商品
					} else if ($mjset['fwtype'] == 2) {
						//查询他商品消费累计
						$sum_money = Db::name('shop_order_goods')
							->alias('sog')
							->join('shop_order so', 'so.id   = sog.orderid')
							->join('shop_product sp', 'sp.id = sog.proid')
							->where('sog.mid', mid)
							->where('so.status', 3)
							->where('sog.proid', 'in', $mjset['productids'])
							->sum('sog.totalprice');

						//商品退款累计
						$refund_money = Db::name('shop_refund_order_goods')
							->alias('srog')
							->join('shop_order so', 'so.id   = srog.orderid')
							->join('shop_product sp', 'sp.id = srog.proid')
							->where('srog.mid', mid)
							->where('so.status', 3)
							->where('so.refund_status', 2)
							->where('srog.proid', 'in', $mjset['productids'])
							->sum('srog.refund_money');
						//所有
					} else {
						//查询他累计消费多少
						$sum_money = Db::name('shop_order')->where('mid', mid)->where('status', 3)->sum('totalprice');
						$refund_money = Db::name('shop_order')->where('mid', mid)->where('status', 3)->where('refund_status', 2)->sum('refund_money');
					}
					$sj_money = $sum_money - $refund_money;
					$sj_money = round($sj_money, 2);
					$all_price = $sj_money + $mj_price;
					foreach ($mjdata as $give) {
						if ($all_price * 1 >= $give['money'] * 1 && $give['money'] * 1 > $moneyduan) {
							$moneyduan = $give['money'] * 1;
							$manjian_money = $give['jian'] * 1;
						}
					}
				} else {
					foreach ($mjdata as $give) {
						if ($mj_price * 1 >= $give['money'] * 1 && $give['money'] * 1 > $moneyduan) {
							$moneyduan = $give['money'] * 1;
							$manjian_money = $give['jian'] * 1;
						}
					}
				}
			}
			if ($manjian_money <= 0) {
				$manjian_money = 0;
			}

			$totalprice = $totalprice - $manjian_money;
			if ($totalprice < 0) {
				$totalprice = 0;
			}

			//运费
			$freight_price = 0;
			if ($data['freight_id']) {
				$freight = Db::name('freight')->where('aid', aid)->where('id', $data['freight_id'])->find();
				if ($freight['pstype'] == 11) {
					$freight['type11key'] = $data['type11key'];
				}
				if ($freight['minpriceset'] == 1 && $freight['minprice'] > 0 && $freight['minprice'] > $product_price) {
					return $this->json(['status' => 0, 'msg' => $freight['name'] . '满' . $freight['minprice'] . '元起送']);
				}
				if (($address['name'] == '' || $address['tel'] == '') && ($freight['pstype'] == 1 || $freight['pstype'] == 3) && $freight['needlinkinfo'] == 1) {
					return $this->json(['status' => 0, 'msg' => '请填写联系人和联系电话']);
				}

				$rs = \app\model\Freight::getFreightPrice($freight, $address, $product_price, $totalnum, $totalweight);
				if ($rs['status'] == 0) {
					return $this->json($rs);
				}

				$freight_price = $rs['freight_price'];
				//判断配送时间选择是否符合要求
				if ($freight['pstimeset'] == 1) {
					$freight_times = explode('~', $data['freight_time']);
					if ($freight_times[1]) {
						$freighttime = strtotime(explode(' ', $freight_times[0])[0] . ' ' . $freight_times[1]);
					} else {
						$freighttime = strtotime($freight_times[0]);
					}
					if (time() + $freight['psprehour'] * 3600 > $freighttime) {
						return $this->json(['status' => 0, 'msg' => (($freight['pstype'] == 0 || $freight['pstype'] == 2 || $freight['pstype'] == 10) ? '配送' : '提货') . '时间必须在' . $freight['psprehour'] . '小时之后']);
					}
				}
			} elseif ($product['freighttype'] == 3) {
				$freight = ['id' => 0, 'name' => '自动发货', 'pstype' => 3];
				if (getcustom('cefang') && ($address['name'] == '' || $address['tel'] == '')) {
					return $this->json(['status' => 0, 'msg' => '请填写联系人和联系电话']);
				}
			} elseif ($product['freighttype'] == 4) {
				$freight = ['id' => 0, 'name' => '在线卡密', 'pstype' => 4];
				if (getcustom('cefang') && ($address['name'] == '' || $address['tel'] == '')) {
					return $this->json(['status' => 0, 'msg' => '请填写联系人和联系电话']);
				}
			} else {
				$freight = ['id' => 0, 'name' => '包邮', 'pstype' => 0];
			}
			//优惠券
			$new_freight_price = $freight_price;
			$coupon_money = 0;
			if ($data['couponrid']) {
				$couponrids = explode(',', $data['couponrid']);
				foreach ($couponrids as $couponrid) {
					$couponrecord = Db::name('coupon_record')->where("bid=-1 or bid=" . $data['bid'])->where('aid', aid)->where('mid', mid)->where('id', $couponrid)->find();
					if (!$couponrecord) {
						return $this->json(['status' => 0, 'msg' => '该' . t('优惠券') . '不存在']);
					} elseif ($couponrecord['status'] != 0) {
						return $this->json(['status' => 0, 'msg' => '该' . t('优惠券') . '已使用过了']);
					} elseif ($couponrecord['starttime'] > time()) {
						return $this->json(['status' => 0, 'msg' => '该' . t('优惠券') . '尚未开始使用']);
					} elseif ($couponrecord['endtime'] < time()) {
						return $this->json(['status' => 0, 'msg' => '该' . t('优惠券') . '已过期']);
					} elseif ($couponrecord['minprice'] > $totalprice) {
						return $this->json(['status' => 0, 'msg' => '该' . t('优惠券') . '不符合条件']);
					} elseif ($couponrecord['type'] != 1 && $couponrecord['type'] != 4 && $couponrecord['type'] != 10) {
						return $this->json(['status' => 0, 'msg' => '该' . t('优惠券') . '不符合条件']);
					}

					$couponinfo = Db::name('coupon')->where('aid', aid)->where('id', $couponrecord['couponid'])->find();
					if (empty($couponinfo)) {
						return $this->json(['status' => 0, 'msg' => '该' . t('优惠券') . '不存在或已作废']);
					}
					//0全场通用,1指定类目,2指定商品
					if (!in_array($couponinfo['fwtype'], [0, 1, 2])) {
						return $this->json(['status' => 0, 'msg' => '该' . t('优惠券') . '超出可用范围']);
					}
					if ($couponrecord['from_mid'] == 0 && $couponinfo && $couponinfo['isgive'] == 2) {
						return $this->json(['status' => 0, 'msg' => '该' . t('优惠券') . '仅可转赠']);
					}
					$usetj = explode(',', $couponinfo['usetj']);
					if (!in_array('-1', $usetj) && !in_array($this->member['levelid'], $usetj) && (!in_array('0', $usetj) || $this->member['subscribe'] != 1)) {
						return $this->json(['status' => 0, 'msg' => '该' . t('优惠券') . '不可用']);
					}
					if ($couponinfo['fwtype'] == 2) {
//指定商品可用
						$productids = explode(',', $couponinfo['productids']);
						if (!array_intersect($proids, $productids)) {
							return $this->json(['status' => 0, 'msg' => '该' . t('优惠券') . '指定商品可用']);
						}
						$thistotalprice = 0;
						foreach ($prolist as $k2 => $v2) {
							$product = $v2['product'];
							if (in_array($product['id'], $productids)) {
								$thistotalprice += $v2['guige']['sell_price'] * $v2['num'];
							}
						}
						if ($thistotalprice < $couponinfo['minprice']) {
							return $this->json(['status' => 0, 'msg' => '该' . t('优惠券') . '指定商品未达到' . $couponinfo['minprice'] . '元']);
						}
					}
					if ($couponinfo['fwtype'] == 1) {
//指定类目可用
						$categoryids = explode(',', $couponinfo['categoryids']);
						$clist = Db::name('shop_category')->where('pid', 'in', $categoryids)->select()->toArray();
						foreach ($clist as $kc => $vc) {
							$categoryids[] = $vc['id'];
							$cate2 = Db::name('shop_category')->where('pid', $vc['id'])->find();
							$categoryids[] = $cate2['id'];
						}
						if (!array_intersect($cids, $categoryids)) {
							return $this->json(['status' => 0, 'msg' => '该' . t('优惠券') . '指定分类可用']);
						}
						$thistotalprice = 0;
						foreach ($prolist as $k2 => $v2) {
							$product = $v2['product'];
							if (array_intersect(explode(',', $product['cid']), $categoryids)) {
								$thistotalprice += $v2['guige']['sell_price'] * $v2['num'];
							}
						}
						if ($thistotalprice < $couponinfo['minprice']) {
							return $this->json(['status' => 0, 'msg' => '该' . t('优惠券') . '指定分类未达到' . $couponinfo['minprice'] . '元']);
						}
					}

					Db::name('coupon_record')->where('id', $couponrid)->update(['status' => 1, 'usetime' => time()]);
					if ($couponrecord['type'] == 4) {
//运费抵扣券
						$new_freight_price = 0;
					} elseif ($couponrecord['type'] == 10) {
//折扣券
						if ($couponinfo['fwtype'] == 1 || $couponinfo['fwtype'] == 2) {
							$coupon_money += $thistotalprice * (100 - $couponrecord['discount']) * 0.01;
						} else {
							$coupon_money += $totalprice * (100 - $couponrecord['discount']) * 0.01;
						}
						if ($coupon_money > $totalprice) {
							$coupon_money = $totalprice;
						}

					} else {
						$coupon_money += $couponrecord['money'];
						if ($coupon_money > $totalprice) {
							$coupon_money = $totalprice;
						}

					}
				}
			}
			//促销活动
			$cuxiaomoney = 0;
			if (in_array('multi_promotion', getcustom())) {
				if ($data['cuxiaoid']) {
					foreach ($data['cuxiaoid'] as $cuxiaoid) {
						if ($cuxiaoid > 0) {
							$cuxiaoid = $cuxiaoid;
							$cuxiaoinfo = Db::name('cuxiao')->where("bid=-1 or bid=" . $data['bid'])->where('aid', aid)->where('id', $cuxiaoid)->find();
							if (!$cuxiaoinfo) {
								return $this->json(['status' => 0, 'msg' => '该促销活动不存在']);
							} elseif ($cuxiaoinfo['starttime'] > time()) {
								return $this->json(['status' => 0, 'msg' => '该促销活动尚未开始']);
							} elseif ($cuxiaoinfo['endtime'] < time()) {
								return $this->json(['status' => 0, 'msg' => '该促销活动已结束']);
							} elseif ($cuxiaoinfo['type'] != 5 && $cuxiaoinfo['type'] != 6 && $cuxiaoinfo['minprice'] > $totalprice) {
								return $this->json(['status' => 0, 'msg' => '该促销活动不符合条件']);
							} elseif (($cuxiaoinfo['type'] == 5 || $cuxiaoinfo['type'] == 6) && $cuxiaoinfo['minnum'] > $totalnum) {
								return $this->json(['status' => 0, 'msg' => '该促销活动不符合条件']);
							}
							if ($cuxiaoinfo['fwtype'] == 2) {
//指定商品可用
								$productids = explode(',', $cuxiaoinfo['productids']);
								if (!array_intersect($proids, $productids)) {
									return $this->json(['status' => 0, 'msg' => '该促销活动指定商品可用']);
								}
								if ($cuxiaoinfo['type'] == 1 || $cuxiaoinfo['type'] == 2 || $cuxiaoinfo['type'] == 3 || $cuxiaoinfo['type'] == 4) {
//指定商品是否达到金额要求
									$thistotalprice = 0;
									foreach ($prolist as $k2 => $v2) {
										$product = $v2['product'];
										if (in_array($product['id'], $productids)) {
											$thistotalprice += $v2['guige']['sell_price'] * $v2['num'];
										}
									}
									if ($thistotalprice < $cuxiaoinfo['minprice']) {
										return $this->json(['status' => 0, 'msg' => '该促销活动指定商品总价未达到' . $cuxiaoinfo['minprice'] . '元']);
									}
								}
								if ($cuxiaoinfo['type'] == 6 || $cuxiaoinfo['type'] == 5) {
//指定商品是否达到件数要求
									$thistotalnum = 0;
									foreach ($prolist as $k2 => $v2) {
										$product = $v2['product'];
										if (in_array($product['id'], $productids)) {
											$thistotalnum += $v2['num'];
										}
									}
									if ($thistotalnum < $cuxiaoinfo['minnum']) {
										return $this->json(['status' => 0, 'msg' => '该促销活动指定商品总数未达到' . $cuxiaoinfo['minnum'] . '件']);
									}
								}
							}
							if ($cuxiaoinfo['fwtype'] == 1) {
//指定类目可用
								$categoryids = explode(',', $cuxiaoinfo['categoryids']);
								$clist = Db::name('shop_category')->where('pid', 'in', $categoryids)->select()->toArray();
								foreach ($clist as $kc => $vc) {
									$categoryids[] = $vc['id'];
									$cate2 = Db::name('shop_category')->where('pid', $vc['id'])->find();
									$categoryids[] = $cate2['id'];
								}
								if (!array_intersect($cids, $categoryids)) {
									return $this->json(['status' => 0, 'msg' => '该促销活动指定分类可用']);
								}
								if ($cuxiaoinfo['type'] == 1 || $cuxiaoinfo['type'] == 2 || $cuxiaoinfo['type'] == 3 || $cuxiaoinfo['type'] == 4) {
//指定商品是否达到金额要求
									$thistotalprice = 0;
									foreach ($prolist as $k2 => $v2) {
										$product = $v2['product'];
										if (array_intersect(explode(',', $product['cid']), $categoryids)) {
											$thistotalprice += $v2['guige']['sell_price'] * $v2['num'];
										}
									}
									if ($thistotalprice < $cuxiaoinfo['minprice']) {
										return $this->json(['status' => 0, 'msg' => '该促销活动指定分类总价未达到' . $cuxiaoinfo['minprice'] . '元']);
									}
								}
								if ($cuxiaoinfo['type'] == 6 || $cuxiaoinfo['type'] == 5) {
//指定类目内商品是否达到件数要求
									$thistotalnum = 0;
									foreach ($prolist as $k2 => $v2) {
										$product = $v2['product'];
										if (array_intersect(explode(',', $product['cid']), $categoryids)) {
											$thistotalnum += $v2['num'];
										}
									}
									if ($thistotalnum < $cuxiaoinfo['minnum']) {
										return $this->json(['status' => 0, 'msg' => '该促销活动指定分类总数未达到' . $cuxiaoinfo['minnum'] . '件']);
									}
								}
							}
							if ($cuxiaoinfo['type'] == 1 || $cuxiaoinfo['type'] == 6) {
//满额立减 满件立减
								$manjian_money = $manjian_money + $cuxiaoinfo['money'];
								$cuxiaomoney += $cuxiaoinfo['money'] * -1;
							} elseif ($cuxiaoinfo['type'] == 2) {
//满额赠送
								$cuxiaomoney += 0;
								$product = Db::name('shop_product')->where('aid', aid)->where('id', $cuxiaoinfo['proid'])->find();
								$guige = Db::name('shop_guige')->where('aid', aid)->where('id', $cuxiaoinfo['ggid'])->find();
								if (!$product) {
									return $this->json(['status' => 0, 'msg' => '赠送产品不存在']);
								}

								if (!$guige) {
									return $this->json(['status' => 0, 'msg' => '赠送产品规格不存在']);
								}

								if ($guige['stock'] < 1) {
									return $this->json(['status' => 0, 'msg' => '赠送产品库存不足']);
								}
								$prolist[] = ['product' => $product, 'guige' => $guige, 'num' => 1, 'isSeckill' => 0];
							} elseif ($cuxiaoinfo['type'] == 3) {
//加价换购
								$cuxiaomoney += $cuxiaoinfo['money'];
								$product = Db::name('shop_product')->where('aid', aid)->where('id', $cuxiaoinfo['proid'])->find();
								$guige = Db::name('shop_guige')->where('aid', aid)->where('id', $cuxiaoinfo['ggid'])->find();
								if (!$product) {
									return $this->json(['status' => 0, 'msg' => '换购产品不存在']);
								}

								if (!$guige) {
									return $this->json(['status' => 0, 'msg' => '换购产品规格不存在']);
								}

								if ($guige['stock'] < 1) {
									return $this->json(['status' => 0, 'msg' => '换购产品库存不足']);
								}
								$prolist[] = ['product' => $product, 'guige' => $guige, 'num' => 1, 'isSeckill' => 0];
							} elseif ($cuxiaoinfo['type'] == 4 || $cuxiaoinfo['type'] == 5) {
//满额打折 满件打折
								$cuxiaomoney4 = 0;
								if ($cuxiaoinfo['fwtype'] == 2) {
									$prozkArr = array_combine(explode(',', $cuxiaoinfo['productids']), explode(',', $cuxiaoinfo['prozk']));
									$pronumArr = array_combine(explode(',', $cuxiaoinfo['productids']), explode(',', $cuxiaoinfo['pronum']));
									foreach ($prolist as $k => $v) {
										$product = $v['product'];
										if ($prozkArr[$product['id']]) {
											$prozk = $prozkArr[$product['id']];
										} elseif (isset($prozkArr[$product['id']])) {
											$prozk = $cuxiaoinfo['zhekou'];
										} else {
											$prozk = 10;
										}
										if ($cuxiaoinfo['type'] == 5 && $pronumArr[$product['id']] && intval($pronumArr[$product['id']]) > $v['num']) {
											$prozk = 10;
										}
										$cuxiaomoney4 += $product_priceArr[$k] * (1 - $prozk * 0.1);
									}
								} elseif ($cuxiaoinfo['fwtype'] == 1) {
									//分类
									$categoryPrice = 0;

									foreach ($prolist as $k2 => $v2) {
										$product = $v2['product'];
										$cids2 = explode(',', $product['cid']);
										if (array_intersect($cids2, $categoryids)) {
											$categoryPrice += $v2['guige']['sell_price'] * $v2['num'];
										}
									}
									$cuxiaomoney4 = $categoryPrice * (1 - $cuxiaoinfo['zhekou'] * 0.1);
								} else {
									$cuxiaomoney4 = $totalprice * (1 - $cuxiaoinfo['zhekou'] * 0.1);
								}
								$cuxiaomoney4 = round($cuxiaomoney4, 2);
								$manjian_money = $manjian_money + $cuxiaomoney4;
								$cuxiaomoney += $cuxiaomoney4 * -1;
							} else {
								$cuxiaomoney += 0;
							}
						} else {
							$cuxiaomoney += 0;
						}
					}
				}
			} else {
				if ($data['cuxiaoid'] > 0) {
					$cuxiaoid = $data['cuxiaoid'];
					$cuxiaoinfo = Db::name('cuxiao')->where("bid=-1 or bid=" . $data['bid'])->where('aid', aid)->where('id', $cuxiaoid)->find();
					if (!$cuxiaoinfo) {
						return $this->json(['status' => 0, 'msg' => '该促销活动不存在']);
					} elseif ($cuxiaoinfo['starttime'] > time()) {
						return $this->json(['status' => 0, 'msg' => '该促销活动尚未开始']);
					} elseif ($cuxiaoinfo['endtime'] < time()) {
						return $this->json(['status' => 0, 'msg' => '该促销活动已结束']);
					} elseif ($cuxiaoinfo['type'] != 5 && $cuxiaoinfo['type'] != 6 && $cuxiaoinfo['minprice'] > $totalprice) {
						return $this->json(['status' => 0, 'msg' => '该促销活动不符合条件']);
					} elseif (($cuxiaoinfo['type'] == 5 || $cuxiaoinfo['type'] == 6) && $cuxiaoinfo['minnum'] > $totalnum) {
						return $this->json(['status' => 0, 'msg' => '该促销活动不符合条件']);
					}
					if ($cuxiaoinfo['fwtype'] == 2) {
//指定商品可用
						$productids = explode(',', $cuxiaoinfo['productids']);
						if (!array_intersect($proids, $productids)) {
							return $this->json(['status' => 0, 'msg' => '该促销活动指定商品可用']);
						}
						if ($cuxiaoinfo['type'] == 1 || $cuxiaoinfo['type'] == 2 || $cuxiaoinfo['type'] == 3 || $cuxiaoinfo['type'] == 4) {
//指定商品是否达到金额要求
							$thistotalprice = 0;
							foreach ($prolist as $k2 => $v2) {
								$product = $v2['product'];
								if (in_array($product['id'], $productids)) {
									$thistotalprice += $v2['guige']['sell_price'] * $v2['num'];
								}
							}
							if ($thistotalprice < $cuxiaoinfo['minprice']) {
								return $this->json(['status' => 0, 'msg' => '该促销活动指定商品未达到' . $cuxiaoinfo['minprice'] . '元']);
							}
						}
						if ($cuxiaoinfo['type'] == 6 || $cuxiaoinfo['type'] == 5) {
//指定商品是否达到件数要求
							$thistotalnum = 0;
							foreach ($prolist as $k2 => $v2) {
								$product = $v2['product'];
								if (in_array($product['id'], $productids)) {
									$thistotalnum += $v2['num'];
								}
							}
							if ($thistotalnum < $cuxiaoinfo['minnum']) {
								return $this->json(['status' => 0, 'msg' => '该促销活动指定商品未达到' . $cuxiaoinfo['minnum'] . '件']);
							}
						}
					}
					if ($cuxiaoinfo['fwtype'] == 1) {
//指定类目可用
						$categoryids = explode(',', $cuxiaoinfo['categoryids']);
						$clist = Db::name('shop_category')->where('pid', 'in', $categoryids)->select()->toArray();
						foreach ($clist as $kc => $vc) {
							$categoryids[] = $vc['id'];
							$cate2 = Db::name('shop_category')->where('pid', $vc['id'])->find();
							$categoryids[] = $cate2['id'];
						}
						if (!array_intersect($cids, $categoryids)) {
							return $this->json(['status' => 0, 'msg' => '该促销活动指定分类可用']);
						}
						if ($cuxiaoinfo['type'] == 1 || $cuxiaoinfo['type'] == 2 || $cuxiaoinfo['type'] == 3 || $cuxiaoinfo['type'] == 4) {
//指定商品是否达到金额要求
							$thistotalprice = 0;
							foreach ($prolist as $k2 => $v2) {
								$product = $v2['product'];
								if (array_intersect(explode(',', $product['cid']), $categoryids)) {
									$thistotalprice += $v2['guige']['sell_price'] * $v2['num'];
								}
							}
							if ($thistotalprice < $cuxiaoinfo['minprice']) {
								return $this->json(['status' => 0, 'msg' => '该促销活动指定分类未达到' . $cuxiaoinfo['minprice'] . '元']);
							}
						}
						if ($cuxiaoinfo['type'] == 6 || $cuxiaoinfo['type'] == 5) {
//指定类目内商品是否达到件数要求
							$thistotalnum = 0;
							foreach ($prolist as $k2 => $v2) {
								$product = $v2['product'];
								if (array_intersect(explode(',', $product['cid']), $categoryids)) {
									$thistotalnum += $v2['num'];
								}
							}
							if ($thistotalnum < $cuxiaoinfo['minnum']) {
								return $this->json(['status' => 0, 'msg' => '该促销活动指定分类未达到' . $cuxiaoinfo['minnum'] . '件']);
							}
						}
					}
					if ($cuxiaoinfo['type'] == 1 || $cuxiaoinfo['type'] == 6) {
//满额立减 满件立减
						$manjian_money = $manjian_money + $cuxiaoinfo['money'];
						$cuxiaomoney = $cuxiaoinfo['money'] * -1;
					} elseif ($cuxiaoinfo['type'] == 2) {
//满额赠送
						$cuxiaomoney = 0;
						$product = Db::name('shop_product')->where('aid', aid)->where('id', $cuxiaoinfo['proid'])->find();
						$guige = Db::name('shop_guige')->where('aid', aid)->where('id', $cuxiaoinfo['ggid'])->find();
						if (!$product) {
							return $this->json(['status' => 0, 'msg' => '赠送产品不存在']);
						}

						if (!$guige) {
							return $this->json(['status' => 0, 'msg' => '赠送产品规格不存在']);
						}

						if ($guige['stock'] < 1) {
							return $this->json(['status' => 0, 'msg' => '赠送产品库存不足']);
						}
						$prolist[] = ['product' => $product, 'guige' => $guige, 'num' => 1, 'isSeckill' => 0];
					} elseif ($cuxiaoinfo['type'] == 3) {
//加价换购
						$cuxiaomoney = $cuxiaoinfo['money'];
						$product = Db::name('shop_product')->where('aid', aid)->where('id', $cuxiaoinfo['proid'])->find();
						$guige = Db::name('shop_guige')->where('aid', aid)->where('id', $cuxiaoinfo['ggid'])->find();
						if (!$product) {
							return $this->json(['status' => 0, 'msg' => '换购产品不存在']);
						}

						if (!$guige) {
							return $this->json(['status' => 0, 'msg' => '换购产品规格不存在']);
						}

						if ($guige['stock'] < 1) {
							return $this->json(['status' => 0, 'msg' => '换购产品库存不足']);
						}
						$prolist[] = ['product' => $product, 'guige' => $guige, 'num' => 1, 'isSeckill' => 0];
					} elseif ($cuxiaoinfo['type'] == 4 || $cuxiaoinfo['type'] == 5) {
//满额打折 满件打折
						if ($cuxiaoinfo['fwtype'] == 2) {
							$cuxiaomoney = 0;
							$prozkArr = array_combine(explode(',', $cuxiaoinfo['productids']), explode(',', $cuxiaoinfo['prozk']));
							$pronumArr = array_combine(explode(',', $cuxiaoinfo['productids']), explode(',', $cuxiaoinfo['pronum']));
							foreach ($prolist as $k => $v) {
								$product = $v['product'];
								if ($prozkArr[$product['id']]) {
									$prozk = $prozkArr[$product['id']];
								} elseif (isset($prozkArr[$product['id']])) {
									$prozk = $cuxiaoinfo['zhekou'];
								} else {
									$prozk = 10;
								}
								if ($cuxiaoinfo['type'] == 5 && $pronumArr[$product['id']] && intval($pronumArr[$product['id']]) > $v['num']) {
									$prozk = 10;
								}
								$cuxiaomoney += $product_priceArr[$k] * (1 - $prozk * 0.1);
							}
						} elseif ($v['fwtype'] == 1) {
							//分类
							$categoryPrice = 0;
							foreach ($prolist as $k2 => $v2) {
								$product = $v2['product'];
								$cids2 = explode(',', $product['cid']);
								if (array_intersect($cids2, $categoryids)) {
									$categoryPrice += $v2['guige']['sell_price'] * $v2['num'];
								}
							}
							$cuxiaomoney = $categoryPrice * (1 - $cuxiaoinfo['zhekou'] * 0.1);
						} else {
							$cuxiaomoney = $totalprice * (1 - $cuxiaoinfo['zhekou'] * 0.1);
						}
						$cuxiaomoney = round($cuxiaomoney, 2);
						$manjian_money = $manjian_money + $cuxiaomoney;
						$cuxiaomoney = $cuxiaomoney * -1;
					} else {
						$cuxiaomoney = 0;
					}
					if (getcustom('plug_tengrui')) {
						$tr_check = new \app\common\TengRuiCheck();
						//判断是否是否符合会员认证、会员关系、一户
						$check_cuxiao = $tr_check->check_cuxiao($this->member, $cuxiaoinfo);
						if ($check_cuxiao && $check_cuxiao['status'] == 0) {
							$cuxiaomoney = 0;
						}
						$cuxiao_tr_roomId = $check_cuxiao['tr_roomId'];
					}
				} else {
					$cuxiaomoney = 0;
				}
			}

			$totalprice = $totalprice - $coupon_money + $cuxiaomoney;
			$totalprice = $totalprice + $new_freight_price;

			//发票
			$invoice_money = 0;
			if ($store_info['invoice'] && $store_info['invoice_rate'] > 0 && $data['invoice']) {
				$invoice_money = round($totalprice * $store_info['invoice_rate'] / 100, 2);
				$totalprice = $totalprice + $invoice_money;
			}
			//积分抵扣
			$scoredkscore = 0;
			$scoredk_money = 0;
			if ($post['usescore'] == 1) {
				$adminset = Db::name('admin_set')->where('aid', aid)->find();
				$score2money = $adminset['score2money'];
				$scoredkmaxpercent = $adminset['scoredkmaxpercent'];
				$scorebdkyf = $adminset['scorebdkyf'];
				$scoredk_money = $this->member['score'] * $score2money;
				if ($scorebdkyf == 1) {
//积分不抵扣运费
					if ($scoredk_money > $totalprice - $new_freight_price) {
						$scoredk_money = $totalprice - $new_freight_price;
					}

				} else {
					if ($scoredk_money > $totalprice) {
						$scoredk_money = $totalprice;
					}

				}
				if ($scoremaxtype == 0) {
					if ($scoredkmaxpercent >= 0 && $scoredkmaxpercent < 100 && $scoredk_money > 0 && $scoredk_money > $totalprice * $scoredkmaxpercent * 0.01) {
						$scoredk_money = $totalprice * $scoredkmaxpercent * 0.01;
					}
				} else {
					if ($scoredk_money > $scoredkmaxmoney) {
						$scoredk_money = $scoredkmaxmoney;
					}

				}
				$totalprice = $totalprice - $scoredk_money;
				$totalprice = round($totalprice * 100) / 100;
				if ($scoredk_money > 0) {
					$scoredkscore = intval($scoredk_money / $score2money);
				}
			}

			$orderdata = [];
			$orderdata['aid'] = aid;
			$orderdata['mid'] = mid;
			$orderdata['bid'] = $data['bid'];
			if (count($buydata) > 1) {
				$orderdata['ordernum'] = $ordernum . '_' . $i;
			} else {
				$orderdata['ordernum'] = $ordernum;
			}
			$orderdata['title'] = $title . (count($prodata) > 1 ? '等' : '');

			$orderdata['linkman'] = $address['name'];
			$orderdata['company'] = $address['company'];
			$orderdata['tel'] = $address['tel'];
			$orderdata['area'] = $address['area'];
			$orderdata['address'] = $address['address'];
			$orderdata['longitude'] = $address['longitude'];
			$orderdata['latitude'] = $address['latitude'];
			$orderdata['area2'] = $address['province'] . ',' . $address['city'] . ',' . $address['district'];

			$orderdata['totalprice'] = $totalprice;
			$orderdata['product_price'] = $product_price;
			$orderdata['leveldk_money'] = $leveldk_money; //会员折扣
			$orderdata['manjian_money'] = $manjian_money; //满减活动
			$orderdata['scoredk_money'] = $scoredk_money; //积分抵扣
			$orderdata['coupon_money'] = $coupon_money + $freight_price - $new_freight_price; //优惠券抵扣
			$orderdata['scoredkscore'] = $scoredkscore; //抵扣掉的积分
			$orderdata['balance_price'] = $balance_price; //尾款金额 定制的
			$orderdata['coupon_rid'] = $data['couponrid'];
			$orderdata['freight_price'] = $freight_price; //运费
			$orderdata['invoice_money'] = $invoice_money; //发票
			$orderdata['givescore'] = $givescore;
			$orderdata['givescore2'] = $givescore2;
			if ($freight && ($freight['pstype'] == 0 || $freight['pstype'] == 10)) {
				//快递
				$orderdata['freight_text'] = $freight['name'] . '(' . $freight_price . '元)';
				$orderdata['freight_type'] = $freight['pstype'];
			} elseif ($freight && $freight['pstype'] == 1) {
				//到店自提
				$orderdata['mdid'] = $data['storeid'];
				$mendian = Db::name('mendian')->where('aid', aid)->where('id', $data['storeid'])->find();
				$orderdata['freight_text'] = $freight['name'] . '[' . $mendian['name'] . ']';
				$orderdata['area2'] = $mendian['area'];
				$orderdata['freight_type'] = 1;
			} elseif ($freight && $freight['pstype'] == 5) {
				//门店配送
				$orderdata['mdid'] = $data['storeid'];
				$mendian = Db::name('mendian')->where('aid', aid)->where('id', $data['storeid'])->find();
				$orderdata['freight_text'] = $freight['name'] . '[' . $mendian['name'] . ']';
				$orderdata['area2'] = $mendian['area'];
				$orderdata['freight_type'] = 5;
			} elseif ($freight && $freight['pstype'] == 2) {
				//同城配送
				$orderdata['freight_text'] = $freight['name'] . '(' . $freight_price . '元)';
				$orderdata['freight_type'] = 2;
			} elseif ($freight && $freight['pstype'] == 12) {
				//app配送
				$orderdata['freight_text'] = $freight['name'] . '(' . $freight_price . '元)';
				$orderdata['freight_type'] = 2;
			} elseif ($freight && ($freight['pstype'] == 3 || $freight['pstype'] == 4)) {
				//自动发货 在线卡密
				$orderdata['freight_text'] = $freight['name'];
				$orderdata['freight_type'] = $freight['pstype'];
			} elseif ($freight && $freight['pstype'] == 11) {
				//选择物流配送
				$type11pricedata = json_decode($freight['type11pricedata'], true);
				$orderdata['freight_text'] = $type11pricedata[$freight['type11key']]['name'] . '(' . $freight_price . '元)';
				$orderdata['freight_type'] = $freight['pstype'];
				$orderdata['freight_content'] = jsonEncode($type11pricedata[$freight['type11key']]);
			} else {
				$orderdata['freight_text'] = '包邮';
			}

			if ($sysset['areafenhong_checktype'] == 1 && $orderdata['tel']) {
				$addressrs = getaddressfromtel($orderdata['tel']);
				if ($addressrs && $addressrs['province']) {
					$orderdata['area2'] = $addressrs['province'] . ',' . $addressrs['city'];
				}
			}

			$orderdata['freight_id'] = $freight['id'];
			$orderdata['freight_time'] = $data['freight_time']; //配送时间
			$orderdata['createtime'] = time();
			$orderdata['platform'] = platform;
			$orderdata['hexiao_code'] = random(16);
			$orderdata['hexiao_qr'] = createqrcode(m_url('admin/hexiao/hexiao?type=shop&co=' . $orderdata['hexiao_code']));
			if (getcustom('hexiao_member')) {
				$orderdata['hexiao_code_member'] = mt_rand(1000, 9999);
			}

			if (getcustom('buy_selectmember')) {
				if ($post['checkmemid']) {
					$orderdata['checkmemid'] = $post['checkmemid'];
				}

			}
			if (getcustom('pay_yuanbao')) {
				$orderdata['total_yuanbao'] = $total_yuanbao;
				$orderdata['have_no_yuanbao'] = $have_no_yuanbao;
			}
			if (getcustom('plug_tengrui')) {
				$orderdata['manjian_tr_roomId'] = $manjian_tr_roomId ? $manjian_tr_roomId : 0;
				$orderdata['cuxiao_tr_roomId'] = $cuxiao_tr_roomId ? $cuxiao_tr_roomId : 0;
				$orderdata['cuxiao_money'] = abs($cuxiaomoney);
				$orderdata['cuxiao_id'] = $cuxiaoid ? $cuxiaoid : 0;
			}
			if (getcustom('school_product')) {
				//升级为当前等级时的操作数据
				$levelOrder = Db::name('member_levelup_order')->where('aid', aid)->where('mid', $this->mid)
					->where('levelid', $userlevel['id'])->where('status', 2)->where('title', 'not in', ['自动升级', '续费延期'])
					->order('id desc')->find();
				if ($levelOrder && $levelOrder['school_id']) {
					$orderdata['school_id'] = $levelOrder['school_id'];
					$orderdata['grade_id'] = $levelOrder['grade_id'];
					$orderdata['class_id'] = $levelOrder['class_id'];
				}
			}
			$orderid = Db::name('shop_order')->insertGetId($orderdata);

			\app\model\Freight::saveformdata($orderid, 'shop_order', $freight['id'], $data['formdata'], $extendInput);
			$payorderid = \app\model\Payorder::createorder(aid, $orderdata['bid'], $orderdata['mid'], 'shop', $orderid, $orderdata['ordernum'], $orderdata['title'], $orderdata['totalprice'], $orderdata['scoredkscore']);

			if ($balance_price > 0) {
				$balancedata = [];
				$balancedata['aid'] = aid;
				$balancedata['bid'] = $orderdata['bid'];
				$balancedata['mid'] = $orderdata['mid'];
				$balancedata['orderid'] = $orderid;
				$balancedata['ordernum'] = $orderdata['ordernum'];
				$balancedata['title'] = $orderdata['title'];
				$balancedata['money'] = $orderdata['balance_price'];
				$balancedata['type'] = 'balance';
				$balancedata['score'] = 0;
				$balancedata['createtime'] = time();
				$balancedata['status'] = 0;
				$balance_pay_orderid = Db::name('payorder')->insertGetId($balancedata);
				Db::name('shop_order')->where('id', $orderid)->update(['balance_pay_orderid' => $balance_pay_orderid]);
			}

			//发票
			if ($store_info['invoice'] && $data['invoice']) {
				$invoice = [
					'order_type' => 'shop',
					'orderid' => $orderid,
					'ordernum' => $orderdata['ordernum'],
					'type' => $data['invoice']['invoice_type'] ? $data['invoice']['invoice_type'] : 1,
					'invoice_name' => $data['invoice']['invoice_name'] ? $data['invoice']['invoice_name'] : '个人',
					'name_type' => $data['invoice']['name_type'] ? $data['invoice']['name_type'] : 1,
					'tax_no' => $data['invoice']['tax_no'] ? $data['invoice']['tax_no'] : '',
					'address' => $data['invoice']['address'] ? $data['invoice']['address'] : '',
					'tel' => $data['invoice']['tel'] ? $data['invoice']['tel'] : '',
					'bank_name' => $data['invoice']['bank_name'] ? $data['invoice']['bank_name'] : '',
					'bank_account' => $data['invoice']['bank_account'] ? $data['invoice']['bank_account'] : '',
					'mobile' => $data['invoice']['mobile'] ? $data['invoice']['mobile'] : '',
					'email' => $data['invoice']['email'] ? $data['invoice']['email'] : '',
				];
				$invoice['aid'] = aid;
				$invoice['bid'] = $orderdata['bid'];
				$invoice['create_time'] = time();
				Db::name('invoice')->insertGetId($invoice);
			}

			$alltotalprice += $orderdata['totalprice'];
			$alltotalscore += $orderdata['scoredkscore'];

			//是否是复购
			$hasordergoods = Db::name('shop_order_goods')->where('aid', aid)->where('mid', mid)->where('status', 'in', '1,2,3')->find();
			if ($hasordergoods) {
				$isfg = 1;
			} else {
				$isfg = 0;
			}

			if (getcustom('everyday_hongbao')) {
				$hd = Db::name('hongbao_everyday')->where('aid', aid)->find();
			}
			$istc1 = 0; //设置了按单固定提成时 只将佣金计算到第一个商品里
			$istc2 = 0;
			$istc3 = 0;
			foreach ($prolist as $key => $v) {
				$product = $v['product'];
				$guige = $v['guige'];
				$num = $v['num'];

				$ogdata = [];
				$ogdata['aid'] = aid;
				$ogdata['bid'] = $product['bid'];
				$ogdata['mid'] = mid;
				$ogdata['orderid'] = $orderid;
				$ogdata['ordernum'] = $orderdata['ordernum'];
				$ogdata['proid'] = $product['id'];
				$ogdata['name'] = $product['name'];
				$ogdata['pic'] = $guige['pic'] ? $guige['pic'] : $product['pic'];
				$ogdata['procode'] = $product['procode'];
				$ogdata['barcode'] = $product['barcode'];
				$ogdata['ggid'] = $guige['id'];
				$ogdata['ggname'] = $guige['name'];
				$ogdata['cid'] = $product['cid'];
				$ogdata['num'] = $num;
				$ogdata['cost_price'] = $guige['cost_price'];
				$ogdata['sell_price'] = $guige['sell_price'];
				$ogdata['totalprice'] = $num * $guige['sell_price'];
				$ogdata['total_weight'] = $num * $guige['weight'];
				$ogdata['status'] = 0;
				$ogdata['createtime'] = time();
				$ogdata['isfg'] = $isfg;
				if ($product['to86yk_tid']) {
					$ogdata['to86yk_tid'] = $product['to86yk_tid'];
				}
				if ($product['fenhongset'] == 0) {
					//不参与分红
					$ogdata['isfenhong'] = 2;
				}

				$og_totalprice = $ogdata['totalprice'];
				if ($product['balance'] > 0) {
					$og_totalprice = $og_totalprice * (1 - $product['balance'] * 0.01);
				}

				$allproduct_price = $product_price;
				$og_leveldk_money = 0;
				$og_coupon_money = 0;
				$og_scoredk_money = 0;
				$og_manjian_money = 0;
				if ($allproduct_price > 0 && $og_totalprice > 0) {
					if ($leveldk_money) {
						$og_leveldk_money = $og_totalprice / $allproduct_price * $leveldk_money;
					}
					if ($coupon_money) {
						$og_coupon_money = $og_totalprice / $allproduct_price * $coupon_money;
					}
					if ($scoredk_money) {
						$og_scoredk_money = $og_totalprice / $allproduct_price * $scoredk_money;
					}
					if ($manjian_money) {
						$og_manjian_money = $og_totalprice / $allproduct_price * $manjian_money;
					}
				}
				$ogdata['scoredk_money'] = $og_scoredk_money;
				$ogdata['leveldk_money'] = $og_leveldk_money;
				$ogdata['manjian_money'] = $og_manjian_money;
				$ogdata['coupon_money'] = $og_coupon_money;
				if ($product['bid'] > 0) {
					$totalprice_business = $og_totalprice - $og_manjian_money - $og_coupon_money;
					//商品独立费率
					if ($product['feepercent'] != '' && $product['feepercent'] != null && $product['feepercent'] >= 0) {
						$ogdata['business_total_money'] = $totalprice_business * (100 - $product['feepercent']) * 0.01;
					} else {
						//商户费率
						$ogdata['business_total_money'] = $totalprice_business * (100 - $store_info['feepercent']) * 0.01;
					}
				}

				//计算商品实际金额  商品金额 - 会员折扣 - 积分抵扣 - 满减抵扣 - 优惠券抵扣
				if ($sysset['fxjiesuantype'] == 1 || $sysset['fxjiesuantype'] == 2) {
					$og_totalprice = $og_totalprice - $og_leveldk_money - $og_scoredk_money - $og_manjian_money;
					if ($couponrecord['type'] != 4) {
//运费抵扣券
						$og_totalprice -= $og_coupon_money;
					}
					$og_totalprice = round($og_totalprice, 2);
					if ($og_totalprice < 0) {
						$og_totalprice = 0;
					}

				}
				$ogdata['real_totalprice'] = $og_totalprice; //实际商品销售金额

				//计算佣金的商品金额
				$commission_totalprice = $ogdata['totalprice'];
				if ($sysset['fxjiesuantype'] == 1) {
					//按成交价格
					$commission_totalprice = $ogdata['real_totalprice'];
					if ($commission_totalprice < 0) {
						$commission_totalprice = 0;
					}

				}
				$commission_totalpriceCache = $commission_totalprice;
				if ($sysset['fxjiesuantype'] == 2) {
					//按销售利润
					$commission_totalprice = $ogdata['real_totalprice'] - $guige['cost_price'] * $num;
					if ($commission_totalprice < 0) {
						$commission_totalprice = 0;
					}

				}
				if (getcustom('pay_yuanbao')) {
					$ogdata['yuanbao'] = $product['yuanbao'];
					$ogdata['total_yuanbao'] = $num * $product['yuanbao'];
				}
				if (getcustom('plug_tengrui')) {
					$ogdata['tr_roomId'] = $product['tr_roomId'];
				}
				if (getcustom('product_glass')) {
					$glass_record_id = $v['glass_record_id'];
					if ($glass_record_id) {
						$glassrecord = Db::name('glass_record')->where('aid', aid)->where('id', $glass_record_id)->find();
						if ($glassrecord) {
							$orderglassrecord = [
								'name' => $glassrecord['name'],
								'desc' => $glassrecord['desc'],
								'degress_left' => $glassrecord['degress_left'],
								'degress_right' => $glassrecord['degress_right'],
								'ipd' => $glassrecord['ipd'],
								'correction_left' => $glassrecord['correction_left'],
								'correction_right' => $glassrecord['correction_right'],
								'is_ats' => $glassrecord['is_ats'],
								'ats_left' => $glassrecord['ats_left'],
								'ats_right' => $glassrecord['ats_right'],
								'ats_zright' => $glassrecord['ats_zright'],
								'ats_zleft' => $glassrecord['ats_zleft'],
								'type' => $glassrecord['type'],
								'mid' => $glassrecord['mid'],
								'createtime' => time(),
								'aid' => aid,
								'bid' => $orderdata['bid'] ?? 0,
								'glass_record_id' => $glassrecord['id'],
							];
							$order_glass_record_id = Db::name('order_glass_record')->insertGetId($orderglassrecord);
							$ogdata['glass_record_id'] = $order_glass_record_id;
						}
					}
				}
				$ogid = Db::name('shop_order_goods')->insertGetId($ogdata);
				$parent1 = [];
				$parent2 = [];
				$parent3 = [];
				$parent4 = [];
				$ogupdate = [];
				$agleveldata = Db::name('member_level')->where('aid', aid)->where('id', $this->member['levelid'])->find();
				if ($agleveldata['can_agent'] > 0 && $agleveldata['commission1own'] == 1) {
					$this->member['pid'] = mid;
				}
				if ($product['commissionset'] != -1) {
					if ($this->member['pid']) {
						$parent1 = Db::name('member')->where('aid', aid)->where('id', $this->member['pid'])->find();
						if ($parent1) {
							$agleveldata1 = Db::name('member_level')->where('aid', aid)->where('id', $parent1['levelid'])->find();
							if ($agleveldata1['can_agent'] != 0 && (!$agleveldata1['commission_appointlevelid'] || in_array($this->member['levelid'], explode(',', $agleveldata1['commission_appointlevelid'])))) {
								$ogupdate['parent1'] = $parent1['id'];
							}
						}
					}
					if ($parent1['pid']) {
						$parent2 = Db::name('member')->where('aid', aid)->where('id', $parent1['pid'])->find();
						if ($parent2) {
							$agleveldata2 = Db::name('member_level')->where('aid', aid)->where('id', $parent2['levelid'])->find();
							if ($agleveldata2['can_agent'] > 1 && (!$agleveldata2['commission_appointlevelid'] || in_array($this->member['levelid'], explode(',', $agleveldata2['commission_appointlevelid'])))) {
								$ogupdate['parent2'] = $parent2['id'];
							}
						}
					}
					if ($parent2['pid']) {
						$parent3 = Db::name('member')->where('aid', aid)->where('id', $parent2['pid'])->find();
						if ($parent3) {
							$agleveldata3 = Db::name('member_level')->where('aid', aid)->where('id', $parent3['levelid'])->find();
							if ($agleveldata3['can_agent'] > 2 && (!$agleveldata3['commission_appointlevelid'] || in_array($this->member['levelid'], explode(',', $agleveldata3['commission_appointlevelid'])))) {
								$ogupdate['parent3'] = $parent3['id'];
							}
						}
					}
					if ($parent3['pid']) {
						$parent4 = Db::name('member')->where('aid', aid)->where('id', $parent3['pid'])->find();
						if ($parent4) {
							$agleveldata4 = Db::name('member_level')->where('aid', aid)->where('id', $parent4['levelid'])->find();
							if ($product['commissionpingjiset'] != 0) {
								if ($product['commissionpingjiset'] == 1) {
									$commissionpingjidata1 = json_decode($product['commissionpingjidata1'], true);
									$agleveldata4['commission_parent_pj'] = $commissionpingjidata1[$agleveldata4['id']];
								} elseif ($product['commissionpingjiset'] == 2) {
									$commissionpingjidata2 = json_decode($product['commissionpingjidata2'], true);
									$agleveldata4['commission_parent_pj'] = $commissionpingjidata2[$agleveldata4['id']];
								} else {
									$agleveldata4['commission_parent_pj'] = 0;
								}
							}
							//持续推荐奖励
							if ($agleveldata4['can_agent'] > 0 && ($agleveldata4['commission_parent'] > 0 || ($parent4['levelid'] == $parent3['levelid'] && $agleveldata4['commission_parent_pj'] > 0))) {
								$ogupdate['parent4'] = $parent4['id'];
							}
						}
					}
					if ($product['commissionset'] == 1) {
//按商品设置的分销比例
						$commissiondata = json_decode($product['commissiondata1'], true);
						if ($commissiondata) {
							if ($agleveldata1) {
								$ogupdate['parent1commission'] = $commissiondata[$agleveldata1['id']]['commission1'] * $commission_totalprice * 0.01;
							}

							if ($agleveldata2) {
								$ogupdate['parent2commission'] = $commissiondata[$agleveldata2['id']]['commission2'] * $commission_totalprice * 0.01;
							}

							if ($agleveldata3) {
								$ogupdate['parent3commission'] = $commissiondata[$agleveldata3['id']]['commission3'] * $commission_totalprice * 0.01;
							}

						}
					} elseif ($product['commissionset'] == 2) {
//按固定金额
						$commissiondata = json_decode($product['commissiondata2'], true);
						if ($commissiondata) {
							if (getcustom('fengdanjiangli') && $product['fengdanjiangli']) {

							} else {
								if ($agleveldata1) {
									$ogupdate['parent1commission'] = $commissiondata[$agleveldata1['id']]['commission1'] * $num;
								}

								if ($agleveldata2) {
									$ogupdate['parent2commission'] = $commissiondata[$agleveldata2['id']]['commission2'] * $num;
								}

								if ($agleveldata3) {
									$ogupdate['parent3commission'] = $commissiondata[$agleveldata3['id']]['commission3'] * $num;
								}

							}
						}
					} elseif ($product['commissionset'] == 3) {
//提成是积分
						$commissiondata = json_decode($product['commissiondata3'], true);
						if ($commissiondata) {
							if ($agleveldata1) {
								$ogupdate['parent1score'] = $commissiondata[$agleveldata1['id']]['commission1'] * $num;
							}

							if ($agleveldata2) {
								$ogupdate['parent2score'] = $commissiondata[$agleveldata2['id']]['commission2'] * $num;
							}

							if ($agleveldata3) {
								$ogupdate['parent3score'] = $commissiondata[$agleveldata3['id']]['commission3'] * $num;
							}

						}
					} else {
						//按会员等级设置的分销比例
						if ($agleveldata1) {
							if (getcustom('plug_ttdz') && $isfg == 1) {
								$agleveldata1['commission1'] = $agleveldata1['commission4'];
							}
							if ($agleveldata1['commissiontype'] == 1) {
								//固定金额按单
								if ($istc1 == 0) {
									$ogupdate['parent1commission'] = $agleveldata1['commission1'];
									$istc1 = 1;
								}
							} else {
								$ogupdate['parent1commission'] = $agleveldata1['commission1'] * $commission_totalprice * 0.01;
							}
						}
						if ($agleveldata2) {
							if (getcustom('plug_ttdz') && $isfg == 1) {
								$agleveldata2['commission2'] = $agleveldata2['commission5'];
							}
							if ($agleveldata2['commissiontype'] == 1) {
								if ($istc2 == 0) {
									$ogupdate['parent2commission'] = $agleveldata2['commission2'];
									$istc2 = 1;
									//持续推荐奖励
									if ($agleveldata2['commission_parent'] > 0 && $ogupdate['parent1']) {
										$ogupdate['parent2commission'] = $ogupdate['parent2commission'] + $agleveldata2['commission_parent'];
									}
								}
							} else {
								$ogupdate['parent2commission'] = $agleveldata2['commission2'] * $commission_totalprice * 0.01;
								//持续推荐奖励
								if ($agleveldata2['commission_parent'] > 0 && $ogupdate['parent1commission'] > 0 && $ogupdate['parent1']) {
									$ogupdate['parent2commission'] = $ogupdate['parent2commission'] + $ogupdate['parent1commission'] * $agleveldata2['commission_parent'] * 0.01;
								}
							}

						}
						if ($agleveldata3) {
							if (getcustom('plug_ttdz') && $isfg == 1) {
								$agleveldata3['commission3'] = $agleveldata3['commission6'];
							}

							if ($agleveldata3['commissiontype'] == 1) {
								if ($istc3 == 0) {
									$ogupdate['parent3commission'] = $agleveldata3['commission3'];
									$istc3 = 1;
									//持续推荐奖励
									if ($agleveldata3['commission_parent'] > 0 && $ogupdate['parent2']) {
										$ogupdate['parent3commission'] = $ogupdate['parent3commission'] + $agleveldata3['commission_parent'];
									}
								}
							} else {
								$ogupdate['parent3commission'] = $agleveldata3['commission3'] * $commission_totalprice * 0.01;
								//持续推荐奖励
								if ($agleveldata3['commission_parent'] > 0 && $ogupdate['parent2commission'] > 0 && $ogupdate['parent2']) {
									$ogupdate['parent3commission'] = $ogupdate['parent3commission'] + $ogupdate['parent2commission'] * $agleveldata3['commission_parent'] * 0.01;
								}
							}
						}
						//持续推荐奖励
						if ($agleveldata4['commission_parent'] > 0 && $ogupdate['parent3']) {
							if ($agleveldata3['commissiontype'] == 1) {
								$ogupdate['parent4commission'] = $agleveldata4['commission_parent'];
							} else {
								$ogupdate['parent4commission'] = $ogupdate['parent3commission'] * $agleveldata4['commission_parent'] * 0.01;
							}
						}
					}

					//平级奖
					if ($agleveldata4 && $ogupdate['parent3'] && $ogupdate['parent3commission'] > 0 && $agleveldata3['id'] == $agleveldata4['id']) {
						$agleveldata4['commissionpingjitype'] = 0;
						if ($product['commissionpingjiset'] != 0) {
							if ($product['commissionpingjiset'] == 1) {
								$commissionpingjidata1 = json_decode($product['commissionpingjidata1'], true);
								$agleveldata4['commission_parent_pj'] = $commissionpingjidata1[$agleveldata4['id']]['commission'];
							} elseif ($product['commissionpingjiset'] == 2) {
								$commissionpingjidata2 = json_decode($product['commissionpingjidata2'], true);
								$agleveldata4['commission_parent_pj'] = $commissionpingjidata2[$agleveldata4['id']]['commission'];
								$agleveldata4['commissionpingjitype'] = 1;
							} else {
								$agleveldata4['commission_parent_pj'] = 0;
							}
						}
						if ($agleveldata4['commission_parent_pj'] > 0) {
							if ($agleveldata4['commissionpingjitype'] == 0) {
								$ogupdate['parent4commission'] = $ogupdate['parent3commission'] * $agleveldata4['commission_parent_pj'] * 0.01;
							} else {
								$ogupdate['parent4commission'] = $agleveldata4['commission_parent_pj'];
							}
							$ogupdate['parent4'] = $parent4['id'];
						}
					}
					if ($agleveldata3 && $ogupdate['parent2'] && $ogupdate['parent2commission'] > 0 && $agleveldata2['id'] == $agleveldata3['id']) {
						$agleveldata3['commissionpingjitype'] = 0;
						if ($product['commissionpingjiset'] != 0) {
							if ($product['commissionpingjiset'] == 1) {
								$commissionpingjidata1 = json_decode($product['commissionpingjidata1'], true);
								$agleveldata3['commission_parent_pj'] = $commissionpingjidata1[$agleveldata3['id']]['commission'];
							} elseif ($product['commissionpingjiset'] == 2) {
								$commissionpingjidata2 = json_decode($product['commissionpingjidata2'], true);
								$agleveldata3['commission_parent_pj'] = $commissionpingjidata2[$agleveldata3['id']]['commission'];
								$agleveldata3['commissionpingjitype'] = 1;
							} else {
								$agleveldata3['commission_parent_pj'] = 0;
							}
						}
						if ($agleveldata3['commission_parent_pj'] > 0) {
							if (!$ogupdate['parent3']) {
								$ogupdate['parent3commission'] = 0;
								$ogupdate['parent3'] = $parent3['id'];
							}
							if ($agleveldata3['commissionpingjitype'] == 0) {
								$ogupdate['parent3commission'] = $ogupdate['parent3commission'] + $ogupdate['parent2commission'] * $agleveldata3['commission_parent_pj'] * 0.01;
							} else {
								$ogupdate['parent3commission'] = $ogupdate['parent3commission'] + $agleveldata3['commission_parent_pj'];
							}
						}
					}
					if ($agleveldata2 && $ogupdate['parent1'] && $ogupdate['parent1commission'] > 0 && $agleveldata1['id'] == $agleveldata2['id']) {
						$agleveldata2['commissionpingjitype'] = 0;
						if ($product['commissionpingjiset'] != 0) {
							if ($product['commissionpingjiset'] == 1) {
								$commissionpingjidata1 = json_decode($product['commissionpingjidata1'], true);
								$agleveldata2['commission_parent_pj'] = $commissionpingjidata1[$agleveldata2['id']]['commission'];
							} elseif ($product['commissionpingjiset'] == 2) {
								$commissionpingjidata2 = json_decode($product['commissionpingjidata2'], true);
								$agleveldata2['commission_parent_pj'] = $commissionpingjidata2[$agleveldata2['id']]['commission'];
								$agleveldata2['commissionpingjitype'] = 1;
							} else {
								$agleveldata2['commission_parent_pj'] = 0;
							}
						}
						if ($agleveldata2['commission_parent_pj'] > 0) {
							if (!$ogupdate['parent2']) {
								$ogupdate['parent2commission'] = 0;
								$ogupdate['parent2'] = $parent2['id'];
							}
							if ($agleveldata2['commissionpingjitype'] == 0) {
								$ogupdate['parent2commission'] = $ogupdate['parent2commission'] + $ogupdate['parent1commission'] * $agleveldata2['commission_parent_pj'] * 0.01;
							} else {
								$ogupdate['parent2commission'] = $ogupdate['parent2commission'] + $agleveldata2['commission_parent_pj'];
							}
						}
					}
				}
				if ($ogupdate) {
					Db::name('shop_order_goods')->where('id', $ogid)->update($ogupdate);
				}

				$totalcommission = 0;
				if ($product['commissionset'] != 4) {
					if (getcustom('plug_ttdz') && $isfg == 1) {
						if ($ogupdate['parent1'] && ($ogupdate['parent1commission'] || $ogupdate['parent1score'])) {
							Db::name('member_commission_record')->insert(['aid' => aid, 'mid' => $ogupdate['parent1'], 'frommid' => mid, 'orderid' => $orderid, 'ogid' => $ogid, 'type' => 'shop', 'commission' => $ogupdate['parent1commission'], 'score' => $ogupdate['parent1score'], 'remark' => '下级复购奖励', 'createtime' => time()]);
							$totalcommission += $ogupdate['parent1commission'];
						}
						if ($ogupdate['parent2'] && ($ogupdate['parent2commission'] || $ogupdate['parent2score'])) {
							Db::name('member_commission_record')->insert(['aid' => aid, 'mid' => $ogupdate['parent2'], 'frommid' => mid, 'orderid' => $orderid, 'ogid' => $ogid, 'type' => 'shop', 'commission' => $ogupdate['parent2commission'], 'score' => $ogupdate['parent2score'], 'remark' => '下二级复购奖励', 'createtime' => time()]);
							$totalcommission += $ogupdate['parent2commission'];
						}
						if ($ogupdate['parent3'] && ($ogupdate['parent3commission'] || $ogupdate['parent3score'])) {
							Db::name('member_commission_record')->insert(['aid' => aid, 'mid' => $ogupdate['parent3'], 'frommid' => mid, 'orderid' => $orderid, 'ogid' => $ogid, 'type' => 'shop', 'commission' => $ogupdate['parent3commission'], 'score' => $ogupdate['parent3score'], 'remark' => '下三级复购奖励', 'createtime' => time()]);
							$totalcommission += $ogupdate['parent3commission'];
						}
					} else {
						if ($ogupdate['parent1'] && ($ogupdate['parent1commission'] || $ogupdate['parent1score'])) {
							Db::name('member_commission_record')->insert(['aid' => aid, 'mid' => $ogupdate['parent1'], 'frommid' => mid, 'orderid' => $orderid, 'ogid' => $ogid, 'type' => 'shop', 'commission' => $ogupdate['parent1commission'], 'score' => $ogupdate['parent1score'], 'remark' => '下级购买商品奖励', 'createtime' => time()]);
							$totalcommission += $ogupdate['parent1commission'];
						}
						if ($ogupdate['parent2'] && ($ogupdate['parent2commission'] || $ogupdate['parent2score'])) {
							Db::name('member_commission_record')->insert(['aid' => aid, 'mid' => $ogupdate['parent2'], 'frommid' => mid, 'orderid' => $orderid, 'ogid' => $ogid, 'type' => 'shop', 'commission' => $ogupdate['parent2commission'], 'score' => $ogupdate['parent2score'], 'remark' => '下二级购买商品奖励', 'createtime' => time()]);
							$totalcommission += $ogupdate['parent2commission'];
						}
						if ($ogupdate['parent3'] && ($ogupdate['parent3commission'] || $ogupdate['parent3score'])) {
							Db::name('member_commission_record')->insert(['aid' => aid, 'mid' => $ogupdate['parent3'], 'frommid' => mid, 'orderid' => $orderid, 'ogid' => $ogid, 'type' => 'shop', 'commission' => $ogupdate['parent3commission'], 'score' => $ogupdate['parent3score'], 'remark' => '下三级购买商品奖励', 'createtime' => time()]);
							$totalcommission += $ogupdate['parent3commission'];
						}
						if ($ogupdate['parent4'] && ($ogupdate['parent4commission'])) {
							Db::name('member_commission_record')->insert(['aid' => aid, 'mid' => $ogupdate['parent4'], 'frommid' => mid, 'orderid' => $orderid, 'ogid' => $ogid, 'type' => 'shop', 'commission' => $ogupdate['parent4commission'], 'score' => 0, 'remark' => '持续推荐奖励', 'createtime' => time()]);
							$totalcommission += $ogupdate['parent4commission'];
						}
					}

					if ($post['checkmemid'] && $commission_totalprice > 0) {
						$checkmember = Db::name('member')->where('aid', aid)->where('id', $post['checkmemid'])->find();
						if ($checkmember) {
							$buyselect_commission = Db::name('member_level')->where('id', $checkmember['levelid'])->value('buyselect_commission');
							$checkmemcommission = $buyselect_commission * $commission_totalprice * 0.01;
							Db::name('member_commission_record')->insert(['aid' => aid, 'mid' => $checkmember['id'], 'frommid' => mid, 'orderid' => $orderid, 'ogid' => $ogid, 'type' => 'shop', 'commission' => $checkmemcommission, 'score' => 0, 'remark' => '购买商品时指定奖励', 'createtime' => time()]);
						}
					}
				}

				if ($product['commissionset4'] == 1 && $product['lvprice'] == 1) {
					//极差分销
					if (getcustom('jicha_removecommission')) {
						//算极差时先减去分销的钱
						$commission_totalpriceCache = $commission_totalpriceCache - $totalcommission;
					}
					if ($this->member['path']) {
						$parentList = Db::name('member')->where('id', 'in', $this->member['path'])->order(Db::raw('field(id,' . $this->member['path'] . ')'))->select()->toArray();
						if ($parentList) {
							$parentList = array_reverse($parentList);
							$lvprice_data = json_decode($guige['lvprice_data'], true);
							$nowprice = $commission_totalpriceCache;
							$giveidx = 0;
							foreach ($parentList as $k => $parent) {
								if ($parent['levelid'] && $lvprice_data[$parent['levelid']]) {
									$thisprice = floatval($lvprice_data[$parent['levelid']]) * $num;
									if ($nowprice > $thisprice) {
										$commission = $nowprice - $thisprice;
										$nowprice = $thisprice;
										$giveidx++;
										//if($giveidx <=3){
										//	$ogupdate['parent'.$giveidx] = $parent['id'];
										//	$ogupdate['parent'.$giveidx.'commission'] = $commission;
										//}
										Db::name('member_commission_record')->insert(['aid' => aid, 'mid' => $parent['id'], 'frommid' => mid, 'orderid' => $orderid, 'ogid' => $ogid, 'type' => 'shop', 'commission' => $commission, 'score' => 0, 'remark' => '下级购买商品差价', 'createtime' => time()]);
									}
								}
							}
						}
					}
				}

				if (getcustom('product_commission_mid')) {
					if ($product['commission_mid']) {
						//商品指定会员佣金奖励
						$commission_mid = json_decode($product['commission_mid'], true);
						$commission_mid = array_filter($commission_mid);
						foreach ($commission_mid as $kcm => $vcm) {
							$money = 0;
							if ($vcm['mid'] && ($vcm['percent'] > 0 || $vcm['money'] > 0)) {
								if ($vcm['percent'] > 0) {
									$money += $commission_totalprice * $vcm['percent'] / 100;
								}

								$money += $vcm['money'] * $num;
								$money = round($money, 2);
								if ($money > 0) {
									Db::name('member_commission_record')->insert(['aid' => aid, 'mid' => $vcm['mid'], 'frommid' => mid, 'orderid' => $orderid, 'ogid' => $ogid, 'type' => 'shop', 'commission' => $money, 'score' => 0, 'remark' => $vcm['name'], 'createtime' => time()]);
								}
							}
						}
					}
				}

				if (getcustom('commission_givedown')) {
					$commission_recordlist = Db::name('member_commission_record')->field("mid,sum(commission) totalcommission")->where('aid', aid)->where('orderid', $orderid)->where('ogid', $ogid)->where('type', 'shop')->where('commission', '>', 0)->group('mid')->select()->toArray();
					foreach ($commission_recordlist as $record) {
						$thismember = Db::name('member')->where('id', $record['mid'])->find();
						$memberlevel = Db::name('member_level')->where('id', $thismember['levelid'])->find();
						if ($memberlevel && ($memberlevel['givedown_percent'] > 0 || $memberlevel['givedown_commission'] > 0)) {
							$downmemberlist = Db::name('member')->where('aid', aid)->where('pid', $record['mid'])->select()->toArray();
							if (!$downmemberlist) {
								continue;
							}

							$downcommission = $memberlevel['givedown_commission'] / count($downmemberlist) + $record['totalcommission'] * $memberlevel['givedown_percent'] * 0.01 / count($downmemberlist);
							foreach ($downmemberlist as $downmember) {
								Db::name('member_commission_record')->insert(['aid' => aid, 'mid' => $downmember['id'], 'frommid' => $thismember['id'], 'orderid' => $orderid, 'ogid' => $ogid, 'type' => 'shop', 'commission' => $downcommission, 'score' => 0, 'remark' => $memberlevel['givedown_txt'], 'createtime' => time()]);
							}
						}
					}
				}

				if (getcustom('everyday_hongbao')) {
					$hongbaoEdu = 0;
					if ($product['everyday_hongbao_bl'] === null) {
						$hongbaoEdu = $og_totalprice * $hd['shop_product_hongbao_bl'] / 100;
					} elseif ($product['everyday_hongbao_bl'] > 0) {
						$hongbaoEdu = $og_totalprice * $product['everyday_hongbao_bl'] / 100;
					}
					$hongbaoEdu = round($hongbaoEdu, 2);
					if ($hongbaoEdu > 0) {
						Db::name('shop_order_goods')->where('id', $ogid)->update(['hongbaoEdu' => $hongbaoEdu]);
					}

				}
				//删除购物车
				Db::name('shop_cart')->where('aid', aid)->where('mid', mid)->where('ggid', $guige['id'])->where('proid', $product['id'])->delete();
				//减库存
				Db::name('shop_guige')->where('aid', aid)->where('id', $guige['id'])->update(['stock' => Db::raw("stock-$num"), 'sales' => Db::raw("sales+$num")]);
				Db::name('shop_product')->where('aid', aid)->where('id', $product['id'])->update(['stock' => Db::raw("stock-$num"), 'sales' => Db::raw("sales+$num")]);
				if (getcustom('guige_split')) {
					\app\model\ShopProduct::declinkstock($product['id'], $guige['id'], $num);
				}
			}

			//公众号通知 订单提交成功
			$tmplcontent = [];
			$tmplcontent['first'] = '有新订单提交成功';
			$tmplcontent['remark'] = '点击进入查看~';
			$tmplcontent['keyword1'] = $store_name; //店铺
			$tmplcontent['keyword2'] = date('Y-m-d H:i:s', $orderdata['createtime']); //下单时间
			$tmplcontent['keyword3'] = $orderdata['title']; //商品
			$tmplcontent['keyword4'] = $orderdata['totalprice'] . '元'; //金额
			\app\common\Wechat::sendhttmpl(aid, $orderdata['bid'], 'tmpl_orderconfirm', $tmplcontent, m_url('admin/order/shoporder'), $orderdata['mdid']);

			$tmplcontent = [];
			$tmplcontent['thing11'] = $orderdata['title'];
			$tmplcontent['character_string2'] = $orderdata['ordernum'];
			$tmplcontent['phrase10'] = '待付款';
			$tmplcontent['amount13'] = $orderdata['totalprice'] . '元';
			$tmplcontent['thing27'] = $this->member['nickname'];
			\app\common\Wechat::sendhtwxtmpl(aid, $orderdata['bid'], 'tmpl_orderconfirm', $tmplcontent, 'admin/order/shoporder', $orderdata['mdid']);
		}
		if (count($buydata) > 1) {
			//创建合并支付单
			$payorderid = \app\model\Payorder::createorder(aid, 0, mid, 'shop_hb', $orderid, $ordernum, $orderdata['title'], $alltotalprice, $alltotalscore);
		}

		return $this->json(['status' => 1, 'payorderid' => $payorderid, 'msg' => '提交成功']);
	}
	public function memberSearch() {
		$search = input('param.diqu');
		if ($search) {
			$search = explode(',', $search);
			$where = [];
			$where[] = ['aid', '=', aid];
			if ($search[0] != '全部') {
				$where[] = ['province', '=', $search[0]];
			}
			if ($search[1] != '全部') {
				$where[] = ['city', '=', $search[1]];
			}
			if ($search[2] != '全部') {
				$where[] = ['area', '=', $search[2]];
			}

			$levelids = Db::name('member_level')->where('aid', aid)->where('can_buyselect', 1)->column('id');
			$where[] = ['levelid', 'in', $levelids];
			$memberList = db('member')->field('id,nickname,headimg,tel,province,city,area')->where($where)->select();
			if (count($memberList) > 0) {
				return $this->json(['status' => 1, 'memberList' => $memberList, 'msg' => '获取成功']);
			} else {
				return $this->json(['status' => 0, 'memberList' => [], 'msg' => '无符合条件用户']);
			}
		}
	}
	//获取升级申请时填写的资料
	public function getmemberuplvinfo() {
		$mid = input('post.mid');
		$minfo = Db::name('member')->where('id', $mid)->find();
		$info = Db::name('member_levelup_order')->where('mid', $mid)->where('status', 2)->find();
		$data = [['昵称', $minfo['nickname']]];
		for ($i = 0; $i <= 20; $i++) {
			if ($info['form' . $i]) {
				$thisdata = explode('^_^', $info['form' . $i]);
				if ($thisdata[1] !== '') {
					if ($thisdata[0] == '手机号') {
						$thisdata[1] = substr($thisdata[1], 0, 3) . '*****' . substr($thisdata[1], -3);
					}
					$data[] = $thisdata;
				}
			}
		}
		return json(['info' => $data]);
	}

	public function mendian() {
		$id = input('param.id');
		$info = Db::name('mendian')->where('aid', aid)->where('id', $id)->find();
		if (empty($info)) {
			return $this->json(['status' => 0, 'msg' => '门店不存在']);
		}
		if ($info['status'] != 1) {
			return $this->json(['status' => 0, 'msg' => '门店未开启']);
		}

		if ($info['pics']) {
			$info['pics'] = $info['pics'] ? explode(',', $info['pics']) : [];
		}

		return json(['status' => 1, 'info' => $info]);
	}

	public function invoice() {
		$bid = input('param.bid/d');
		$post = input('post.');
		$order_type = input('param.type');

		if ($bid) {
			$invoice = Db::name('business')->where('aid', aid)->where('id', $bid)->find();
		} else {
			$invoice = Db::name('admin_set')->where('aid', aid)->find();
		}
		if (!$invoice['invoice']) {
			return $this->json(['status' => 0, 'msg' => '未开启发票功能']);
		}

		//todo 上次的信息
		$rdata['status'] = 1;
		$rdata['invoice_type'] = explode(',', $invoice['invoice_type']);
		return $this->json($rdata);
	}

	//获取抖音商品id
	public function getDouyinProductId() {
		$proid = input('param.proid/d');
		$douyin_product_id = Db::name('shop_product')->where('id', $proid)->value('douyin_product_id');
		if (!$douyin_product_id) {
			return $this->json(['status' => 0, 'msg' => '获取失败']);
		}

		return $this->json(['status' => 1, 'douyin_product_id' => $douyin_product_id]);
	}

	public function imgsearch() {
		if (getcustom('image_search')) {
			$image_search = Db::name('admin')->where('id', aid)->value('image_search');
			if ($image_search == 1) {
				$set = Db::name('baidu_set')->where('aid', aid)->find();
				if ($set['image_search'] == 1) {
					$rdata['status'] = 1;
					$rdata['info']['banner'] = $set['image_search_banner'];
					return $this->json($rdata);
				}
			}
		}
		return $this->json(['status' => 0, 'msg' => '系统错误']);
	}

	public function getprolistWithImg() {
		if (getcustom('image_search')) {
			$where = [];
			$where[] = ['aid', '=', aid];
			$where[] = ['ischecked', '=', 1];
			if (isdouyin == 1) {
				$where[] = ['douyin_product_id', '<>', ''];
			} else {
				$where[] = ['douyin_product_id', '=', ''];
			}

			//图搜
			$imgurl = input('param.imgurl');
			if ($imgurl) {
				$client = new \app\custom\Baidu(aid);
				$proids = $client->searchProduct($imgurl);
				if ($proids) {
					$where[] = ['id', 'in', $proids];
					$proids = implode(',', $proids);
					$order = Db::raw('field(id,' . $proids . ')');
				} else {
					$order = 'sort desc,id desc';
				}
			}
			if (input('param.field') && input('param.order')) {
				$order = input('param.field') . ' ' . input('param.order') . ',sort,id desc';
			}

			//$where[] = ['status','=',1];
			$nowtime = time();
			$nowhm = date('H:i');
			$where[] = Db::raw("`status`=1 or (`status`=2 and unix_timestamp(start_time)<=$nowtime and unix_timestamp(end_time)>=$nowtime) or (`status`=3 and ((start_hours<end_hours and start_hours<='$nowhm' and end_hours>='$nowhm') or (start_hours>=end_hours and (start_hours<='$nowhm' or end_hours>='$nowhm'))) )");

			if (input('param.bid')) {
				$where[] = ['bid', '=', input('param.bid/d')];
			} else {
				$business_sysset = Db::name('business_sysset')->where('aid', aid)->find();
				if (!$business_sysset || $business_sysset['status'] == 0 || $business_sysset['product_isshow'] == 0) {
					$where[] = ['bid', '=', 0];
				}
			}
			//分类
			if (input('param.cid')) {
				$cid = input('post.cid') ? input('post.cid/d') : input('param.cid/d');
				$where2 = "find_in_set('-1',showtj)";
				if ($this->member) {
					$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
					if ($this->member['subscribe'] == 1) {
						$where2 .= " or find_in_set('0',showtj)";
					}
				}
				$tjwhere[] = Db::raw($where2);
				//子分类
				$clist = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', $cid)->column('id');
				if ($clist) {
					$clist2 = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', 'in', $clist)->column('id');
					$cCate = array_merge($clist, $clist2, [$cid]);
					if ($cCate) {
						$whereCid = [];
						foreach ($cCate as $k => $c2) {
							$whereCid[] = "find_in_set({$c2},cid)";
						}
						$where[] = Db::raw(implode(' or ', $whereCid));
					}
				} else {
					$where[] = Db::raw("find_in_set(" . $cid . ",cid)");
				}
			} else {
				if (getcustom('product_cat_showtj')) {
					$where2 = "find_in_set('-1',showtj)";
					if ($this->member) {
						$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
						if ($this->member['subscribe'] == 1) {
							$where2 .= " or find_in_set('0',showtj)";
						}
					}
					$tjwhere[] = Db::raw($where2);
					$clist = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->column('id');

					if ($clist) {
						$whereCid = [];
						foreach ($clist as $k => $c2) {
							$whereCid[] = "find_in_set({$c2},cid)";
						}
						$where[] = Db::raw(implode(' or ', $whereCid));
					}
				}
			}

			//商家的商品分类
			if (input('param.cid2')) {
				$cid2 = input('post.cid2') ? input('post.cid2/d') : input('param.cid2/d');
				//子分类
				$clist = Db::name('shop_category2')->where('aid', aid)->where('pid', $cid2)->column('id');
				if ($clist) {
					$clist2 = Db::name('shop_category2')->where('aid', aid)->where('pid', 'in', $clist)->column('id');
					$cCate = array_merge($clist, $clist2, [$cid2]);
					if ($cCate) {
						$whereCid = [];
						foreach ($cCate as $k => $c2) {
							$whereCid[] = "find_in_set({$c2},cid2)";
						}
						$where[] = Db::raw(implode(' or ', $whereCid));
					}
				} else {
					$where[] = Db::raw("find_in_set(" . $cid2 . ",cid2)");
				}
			}
			if (input('param.gid')) {
				$where[] = Db::raw("find_in_set(" . intval(input('param.gid')) . ",gid)");
			}

			if (input('param.keyword')) {
				$where[] = ['name', 'like', '%' . input('param.keyword') . '%'];
			}

			$where2 = "find_in_set('-1',showtj)";
			if ($this->member) {
				$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
				if ($this->member['subscribe'] == 1) {
					$where2 .= " or find_in_set('0',showtj)";
				}
			}
			$where[] = Db::raw($where2);

			//优惠券可用商品列表
			$cpid = input('param.cpid/d');
			if ($cpid > 0) {
				$coupon = Db::name('coupon')->where('id', $cpid)->find();
				$where[] = ['bid', '=', $coupon['bid']];
				if ($coupon['fwtype'] == 1) {
					//指定类目
					$categoryids = explode(',', $coupon['categoryids']);
					$clist = Db::name('shop_category')->where('pid', 'in', $categoryids)->select()->toArray();
					foreach ($clist as $kc => $vc) {
						$categoryids[] = $vc['id'];
						$cate2 = Db::name('shop_category')->where('pid', $vc['id'])->find();
						$categoryids[] = $cate2['id'];
					}
					$whereCid = [];
					foreach ($categoryids as $k => $c2) {
						$whereCid[] = "find_in_set({$c2},cid)";
					}
					$where[] = Db::raw(implode(' or ', $whereCid));
				}
				if ($coupon['fwtype'] == 2) {
					//指定商品
					$where[] = ['id', 'in', $coupon['productids']];
				}
			}
			$pernum = 10;
			$pagenum = input('post.pagenum');
			if (!$pagenum) {
				$pagenum = 1;
			}

			$datalist = Db::name('shop_product')->field("id,pic,name,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,price_type")->where($where) /*->page($pagenum,$pernum)*/->order($order)->select()->toArray();
//		$sql = Db::getlastsql();dd($sql);
			if (!$datalist) {
				$datalist = [];
			}

			$datalist = $this->formatprolist($datalist);
			return $this->json(['status' => 1, 'data' => $datalist]);
		}
	}

	public function diylight() {
		if (getcustom('diy_light')) {
			$set = Db::name('diylight_set')->where('aid', aid)->where('mid', 0)->find();
			if ($set['status'] == 1) {
				$rdata['status'] = 1;
				$set['bgimgs'] = $set['bgimgs'] ? explode(',', $set['bgimgs']) : [];

				if (mid > 0) {
					$userimg = Db::name('diylight_set')->where('aid', aid)->where('mid', mid)->order('id', 'desc')->column('bgimgs');
					if ($userimg && $set['bgimgs']) {
						$set['bgimgs'] = array_merge($userimg, $set['bgimgs']);
					}
				}

				$rdata['data']['bgimgs'] = $set['bgimgs'];
				$proid = input('param.id/d');
				if ($proid) {
					$pro = $this->getprolistForLight($proid);
					$pro = $pro->getData();
					$rdata['data']['pro'] = $pro['data'][0];
				}

				return $this->json($rdata);
			}
		}
		return $this->json(['status' => 0, 'msg' => '系统错误']);
	}
	public function diylightUpload() {
		$this->checklogin();
		$imgurl = input('param.imgurl');
		if (empty($imgurl)) {
			return $this->json(['status' => 0, 'msg' => '请上传图片']);
		}
		if (getcustom('diy_light')) {
			$set = Db::name('diylight_set')->where('aid', aid)->where('mid', 0)->find();
			if ($set['status'] == 1) {
				$userimg = Db::name('diylight_set')->where('aid', aid)->where('mid', mid)->order('id', 'asc')->select()->toArray();
				$data = [
					'aid' => aid,
					'mid' => mid,
					'bgimgs' => $imgurl,
				];
				Db::name('diylight_set')->insert($data);
				if (count($userimg) >= 5) {
					Db::name('diylight_set')->where('id', $userimg[0]['id'])->delete();
				}

				return $this->json(['status' => 1, 'msg' => 'ok']);
			}
		}
		return $this->json(['status' => 0, 'msg' => '系统错误']);
	}

	public function getprolistForLight($proid = 0) {
		$where = [];
		$where[] = ['aid', '=', aid];
		$where[] = ['ischecked', '=', 1];
		$where[] = ['diypics', '<>', ''];
		if (isdouyin == 1) {
			$where[] = ['douyin_product_id', '<>', ''];
		} else {
			$where[] = ['douyin_product_id', '=', ''];
		}
		//$where[] = ['status','=',1];
		$nowtime = time();
		$nowhm = date('H:i');
		$where[] = Db::raw("`status`=1 or (`status`=2 and unix_timestamp(start_time)<=$nowtime and unix_timestamp(end_time)>=$nowtime) or (`status`=3 and ((start_hours<end_hours and start_hours<='$nowhm' and end_hours>='$nowhm') or (start_hours>=end_hours and (start_hours<='$nowhm' or end_hours>='$nowhm'))) )");

		if (input('param.bid')) {
			$where[] = ['bid', '=', input('param.bid/d')];
		} else {
			$business_sysset = Db::name('business_sysset')->where('aid', aid)->find();
			if (!$business_sysset || $business_sysset['status'] == 0 || $business_sysset['product_isshow'] == 0) {
				$where[] = ['bid', '=', 0];
			}
		}
		if (input('param.field') && input('param.order')) {
			$order = input('param.field') . ' ' . input('param.order') . ',sort,id desc';
		} else {
			$order = 'sort desc,id desc';
		}
		//分类
		if (input('param.cid')) {
			$cid = input('post.cid') ? input('post.cid/d') : input('param.cid/d');
			$where2 = "find_in_set('-1',showtj)";
			if ($this->member) {
				$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
				if ($this->member['subscribe'] == 1) {
					$where2 .= " or find_in_set('0',showtj)";
				}
			}
			$tjwhere[] = Db::raw($where2);
			//子分类
			$clist = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', $cid)->column('id');
			if ($clist) {
				$clist2 = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->where('pid', 'in', $clist)->column('id');
				$cCate = array_merge($clist, $clist2, [$cid]);
				if ($cCate) {
					$whereCid = [];
					foreach ($cCate as $k => $c2) {
						$whereCid[] = "find_in_set({$c2},cid)";
					}
					$where[] = Db::raw(implode(' or ', $whereCid));
				}
			} else {
				$where[] = Db::raw("find_in_set(" . $cid . ",cid)");
			}
		} else {
			if (getcustom('product_cat_showtj')) {
				$where2 = "find_in_set('-1',showtj)";
				if ($this->member) {
					$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
					if ($this->member['subscribe'] == 1) {
						$where2 .= " or find_in_set('0',showtj)";
					}
				}
				$tjwhere[] = Db::raw($where2);
				$clist = Db::name('shop_category')->where($tjwhere)->where('aid', aid)->column('id');
				if ($clist) {
					$whereCid = [];
					foreach ($clist as $k => $c2) {
						$whereCid[] = "find_in_set({$c2},cid)";
					}
					$where[] = Db::raw(implode(' or ', $whereCid));
				}
			}
		}

		//商家的商品分类
		if (input('param.cid2')) {
			$cid2 = input('post.cid2') ? input('post.cid2/d') : input('param.cid2/d');
			//子分类
			$clist = Db::name('shop_category2')->where('aid', aid)->where('pid', $cid2)->column('id');
			if ($clist) {
				$clist2 = Db::name('shop_category2')->where('aid', aid)->where('pid', 'in', $clist)->column('id');
				$cCate = array_merge($clist, $clist2, [$cid2]);
				if ($cCate) {
					$whereCid = [];
					foreach ($cCate as $k => $c2) {
						$whereCid[] = "find_in_set({$c2},cid2)";
					}
					$where[] = Db::raw(implode(' or ', $whereCid));
				}
			} else {
				$where[] = Db::raw("find_in_set(" . $cid2 . ",cid2)");
			}
		}
		if (input('param.gid')) {
			$where[] = Db::raw("find_in_set(" . intval(input('param.gid')) . ",gid)");
		}

		if (input('param.keyword')) {
			$where[] = ['name', 'like', '%' . input('param.keyword') . '%'];
		}
		if ($proid) {
			$where[] = ['id', '=', $proid];
		}

		$where2 = "find_in_set('-1',showtj)";
		if ($this->member) {
			$where2 .= " or find_in_set('" . $this->member['levelid'] . "',showtj)";
			if ($this->member['subscribe'] == 1) {
				$where2 .= " or find_in_set('0',showtj)";
			}
		}
		$where[] = Db::raw($where2);

		$pernum = 9;
		$pagenum = input('post.pagenum');
		if (!$pagenum) {
			$pagenum = 1;
		}

		$datalist = Db::name('shop_product')->field("id,pic,name,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,price_type,diypics")->where($where)->page($pagenum, $pernum)->order($order)->select()->toArray();
//		$sql = Db::getlastsql();dd($sql);
		if (!$datalist) {
			$datalist = [];
		} else {
			foreach ($datalist as $k => $item) {
				$datalist[$k]['url'] = $item['diypics'] ? explode(',', $item['diypics']) : [];
				$datalist[$k]['realUrl'] = $datalist[$k]['url'] ? $datalist[$k]['url'][0] : '';
			}
		}

		return $this->json(['status' => 1, 'data' => $datalist]);
	}

	function getwxScheme() {
		$this->checklogin();
		$post = input('post.');
		$platform = platform;
		$page = '/pages/shop/product';
		$scene = 'id=' . $post['proid'] . '&pid=' . $this->member['id'];
		$product = Db::name('shop_product')->where('id', $post['proid'])->find();
		//$page =  ltrim($page,'/');
		//var_dump($page);
		//$url = 'https://api.weixin.qq.com/wxa/generatescheme?access_token='.\app\common\Wechat::access_token(aid,'wx');
		$url = 'https://api.weixin.qq.com/wxa/genwxashortlink?access_token=' . \app\common\Wechat::access_token(aid, 'wx');
		$data = array();
		//$data['jump_wxa'] = ['path'=>$page,'query'=>$scene];
		//$data['is_expire'] = true;
		//$data['expire_time'] = time()+86400*30;
		$data['page_url'] = 'pages/shop/product?id=' . $post['proid'] . '&pid=' . $this->member['id'];
		$data['page_title'] = $product['name'];
		$data['is_permanent'] = false;
		$res = request_post($url, jsonEncode($data));
		//var_dump($res);
		$errmsg = json_decode($res, true);
		if ($errmsg['errcode']) {
			if ($errmsg['errcode'] == -1) {
				echojson(array('status' => 0, 'msg' => '系统繁忙，此时请稍候再试'));
			} else {
				echojson(array('status' => 0, 'msg' => $errmsg['errmsg']));
			}
		}

		return $this->json(['status' => 1, 'openlink' => $errmsg['link']]);
	}

}